سؤال

In my web applicaton I use OpenJPA on Apache Tomcat (TomEE)/7.0.37 server. I use Netbeans to auto generate class ("Entity Class from database..." and "Session Beans From Entity Class...").

my User.class:

@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
}

when I create new user by 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);

}

i get exception:

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}"

it's look like I my user id is not correct. What is wrong ?

هل كانت مفيدة؟

المحلول

I think your problem is your id generation type - GenerationType.IDENTITY. Usually when using identity a special database column is used to generate the id. The id is not generated until the data is inserted into the database and the id itself is not available to the entity until after commit. However, Bean Validation occurs on the pre-persist callback using the current state of the entity. This will fail, because the id is still null.

I probably would just change the generation type.

نصائح أخرى

I won't do this:

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

This basically is a misused, if this Role is exist and able to link to User you're creating, you should retrieve it and set; otherwise, if you want to create a new Role, don't set @Id for it, or it will cause error as OpenJPA will look for an existing item instead of creating new one.

And, yes, it's possible the cause of your error. The fix is retrieve Role or just don't set @Id attribute. Plus, .setIdRoles() naming is quite strange naming to me.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top