Hibernate OneToMany插入失败异常

时间:2014-01-23 00:11:46

标签: java hibernate java-ee jpa

坚持使用奇数无法插入值NULL,插入失败例外,提前获取所有帮助的Thnaks。

以下是我的映射:

实体类角色:

@OneToMany(mappedBy = "role")
public Set<Feature> getFeatures() {
    return features;
}

public void setFeatures(Set<Feature> features) {
    this.features = features;
}

功能实体类:

@ManyToOne
@JoinColumn(name = "ROLE_ID", nullable = false)
public Role getRole() {
    return role;
}

public void setRole(Role role) {
    this.role = role;
}

错误:

 Caused by: javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: Cannot insert the value NULL into column 'ROLE_ID', table 'FEATURE'; column does not allow nulls. INSERT fails.
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1361) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Final]
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1289) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Final]
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1295) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Final]
at org.hibernate.ejb.AbstractEntityManagerImpl$CallbackExceptionMapperImpl.mapManagedFlushFailure(AbstractEntityManagerImpl.java:1481) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Final]
at org.hibernate.engine.transaction.synchronization.internal.SynchronizationCallbackCoordinatorImpl.beforeCompletion(SynchronizationCallbackCoordinatorImpl.java:109) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
at org.hibernate.engine.transaction.synchronization.internal.RegisteredSynchronization.beforeCompletion(RegisteredSynchronization.java:53) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
at com.arjuna.ats.internal.jta.resources.arjunacore.SynchronizationImple.beforeCompletion(SynchronizationImple.java:76)
at com.arjuna.ats.arjuna.coordinator.TwoPhaseCoordinator.beforeCompletion(TwoPhaseCoordinator.java:273)
at com.arjuna.ats.arjuna.coordinator.TwoPhaseCoordinator.end(TwoPhaseCoordinator.java:93)
at com.arjuna.ats.arjuna.AtomicAction.commit(AtomicAction.java:164)
at com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionImple.commitAndDisassociate(TransactionImple.java:1165)

是的,我已经调用了getter setter来适当地设置父子对象关系

1 个答案:

答案 0 :(得分:0)

解决了这一点。问题在于两个不同的EntityManager上下文是冲突的,尽管异常与问题无关。