0

在我的网络应用程序中,我在 Apache Tomcat (TomEE)/7.0.37 服务器上使用 OpenJPA。我使用 Netbeans 自动生成类(“来自数据库的实体类......”和“来自实体类的会话 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 不正确。怎么了 ?

4

2 回答 2

1

我认为您的问题是您的 id 生成类型 - GenerationType.IDENTITY。通常在使用身份时,会使用一个特殊的数据库列来生成 id。在将数据插入数据库之前不会生成 id,并且 id 本身在提交之前对实体不可用。但是,Bean Validation 使用实体的当前状态在 pre-persist 回调中发生。这将失败,因为 id 仍然是null.

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

于 2013-09-25T10:46:04.670 回答
0

我不会这样做:

Roles r = new Roles();  
r.setIdRoles(new Short("2"));
List<Roles> roleList = new ArrayList<Roles>();
roleList.add(r);     

这基本上是一个误用,如果这个角色存在并且能够链接到你正在创建的用户,你应该检索它并设置;否则,如果您想创建一个新角色,请不要为其设置@Id,否则会导致错误,因为 OpenJPA 将查找现有项目而不是创建新项目。

而且,是的,这可能是您的错误的原因。修复是检索角色或只是不设置@Id 属性。另外, .setIdRoles() 命名对我来说是很奇怪的命名。

于 2013-09-24T18:46:33.860 回答