gpt4 book ai didi

java - 为什么我不能通过 openjpa 持久化对象? - 验证不正确

转载 作者:行者123 更新时间:2023-12-01 14:04:49 25 4
gpt4 key购买 nike

在我的 Web 应用程序中,我在 Apache Tomcat (TomEE)/7.0.37 服务器上使用 OpenJPA。我使用 Netbeans 自动生成类(“来自数据库的实体类...”和“来自实体类的 session Bean...”)。

我的用户类:

@Entity
@Table(name = "USER")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "User.findAll", query = "SELECT u FROM User u"),
@NamedQuery(name = "User.findByIdUser", query = "SELECT u FROM User u WHERE u.idUser = :idUser"),
@NamedQuery(name = "User.findByLogin", query = "SELECT u FROM User u WHERE u.login = :login"),
@NamedQuery(name = "User.findByPassword", query = "SELECT u FROM User u WHERE u.password = :password")})
public class User implements Serializable {
private static final long serialVersionUID = 1L;


@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@NotNull
@Column(name = "id_user")
private Short idUser;


@Size(max = 8)
@Column(name = "login")
private String login;
@Size(max = 64)
@Column(name = "password")
private String password;
@JoinTable(name = "USER_has_ROLES", joinColumns = {
@JoinColumn(name = "USER_id", referencedColumnName = "id_user")}, inverseJoinColumns = {
@JoinColumn(name = "ROLES_id", referencedColumnName = "id_roles")})
@ManyToMany
private List<Roles> rolesList;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "user")
private List<Lecturer> lecturerList;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "user")
private List<Student> studentList;

//constructors, getters, setters
}

当我通过 ManagedBean 创建新用户时:

private void addUser() {
User user = new User();
user.setLogin(registerLog);
user.setPassword(registerPass);
Roles r = new Roles();
r.setIdRoles(new Short("2"));
List<Roles> roleList = new ArrayList<Roles>();
roleList.add(r);
user.setRolesList(roleList);


userFacade.create(user); //<------here i create by abstract facade by em.persist(user);

}

我得到异常:

javax.el.ELException: javax.ejb.EJBException: The bean encountered a non-application exception; nested exception is: javax.validation.ConstraintViolationException: A validation constraint failure occurred for class "model.entity.User".

viewId=/pages/register.xhtml
location=/home/jakub/Projekty/Collv2/build/web/pages/register.xhtml
phaseId=INVOKE_APPLICATION(5)

Caused by:
javax.validation.ConstraintViolationException - A validation constraint failure occurred for class "model.entity.User".
at org.apache.openjpa.persistence.validation.ValidatorImpl.validate(ValidatorImpl.java:282)

/pages/register.xhtml at line 26 and column 104 action="#{registerController.register}"

看来我的用户 ID 不正确。出了什么问题?

最佳答案

我认为你的问题是你的 id 生成类型 - GenerationType.IDENTITY。通常,当使用身份时,会使用特殊的数据库列来生成 id。直到将数据插入数据库后才会生成 id,并且在提交之后实体才可以使用 id 本身。但是,Bean 验证会使用实体的当前状态在预持久回调中进行。这将失败,因为 id 仍然为 null

我可能只会更改生成类型。

关于java - 为什么我不能通过 openjpa 持久化对象? - 验证不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18988982/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com