【发布时间】:2011-08-30 21:05:41
【问题描述】:
我在尝试使用 JPA 2.0、SpringMvc 3.0 在多对多关系中插入新元素时遇到烦人的错误消息。
我有一张带States 的表格,还有一张带有Persons 的表格。一个人可以与许多国家联系,一个国家可以与许多人联系。 在这种特殊情况下,我有一个 listOfStates 和一个人,我想将这些元素插入到我的多对多关系中。
ManyToMany 关系(在表 STATE 中)
//bi-directional many-to-many association to Appointment
@ManyToMany(cascade=CascadeType.ALL)
@JoinTable(
name="PERSON_STATE"
, joinColumns={
@JoinColumn(name="PERSON_ID", nullable=false)
}
, inverseJoinColumns={
@JoinColumn(name="CODE_STATE", nullable=false)
}
)
我从控制器调用的 DAO 代码
try{
getEntityManager().getTransaction().begin();
getEntityManager().persist(myPerson);
IStateDAO stateDAO = new StateDAO();
for (int i=0; i<listOfStates.length; i++){
State myState = stateDAO.findState(listOfStates[i]);
if (myState != null){
myState.getPersons().add(myPerson);
getEntityManager().persist(myState);
}
}
getEntityManager().getTransaction().commit();
getEntityManager().close();
} catch (RuntimeException re) {
getEntityManager().close();
throw re;
}
有趣的是,当我不从网页插入数据时,这段代码可以正常工作。我在这里做错了什么?我已经在数据库中有一些人和状态。
全栈错误消息:
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is javax.validation.ConstraintViolationException: Bean Validation constraint(s) violated while executing Automatic Bean Validation on callback event:'preUpdate'. Please refer to embedded ConstraintViolations for details.
javax.validation.ConstraintViolationException: Bean Validation constraint(s) violated while executing Automatic Bean Validation on callback event:'preUpdate'. Please refer to embedded ConstraintViolations for details.
任何指针都将不胜感激。先谢谢大家了。
【问题讨论】:
标签: model-view-controller spring jpa