在我的网络应用程序中,我在 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 不正确。怎么了 ?