在我的web應用程序中,我在Apache Tomcat(TomEE)/7.0.37服務器上使用OpenJPA。我使用Netbeans自動生成類(「數據庫中的實體類...」和「實體類中的會話Bean ...」)。爲什麼我不能通過openjpa持久化對象? - 錯誤驗證
我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
}
,當我通過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}"
它看起來像我我的用戶名不相關克拉。哪裏不對 ?
找出它是哪個約束呢?做一些像'User'一個接一個地去除屬性(除了'Id')然後看看哪一個讓錯誤消失。 – millimoose
我試了一下,我知道必須是'@NotNull'註釋,因爲當我刪除它並調試它的代碼時,'em.persist(user)'不會停止',但是在那之後我得到異常:'javax .el.ELException:javax.ejb.EJBTransactionRolledbackException:事務回滾,大概是因爲使用setRollbackOnly是synchronization'在數據庫中調用我還沒有關於添加用戶 – kuba44
任何信息是,所有的輸出你得到?應該有什麼地方以及... – millimoose