Search code examples
javavalidationannotationsbean-validationopenjpa

why i can't persist object by openjpa? - incorrect validation


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 ?


Solution

  • 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.