2013-05-06 122 views
0

我想在jpa中做一個簡單的列表更新,但它不工作。 我有類似的東西。更新合併onetomany jpa /休眠 - ConstraintViolationImpl

@Entity 
@Table(name = "user") 
@DynamicUpdate 
public class User implements Serializable { 

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "user", fetch = FetchType.LAZY) 
    private List<UserLangs> userLangsList; 

} 

和表

@Entity 
@Table(name = "user_langs") 
public class UserLangs implements Serializable { 

    private static final long serialVersionUID = 1L; 

    @Id 
    @GeneratedValue(generator = "foreign") 
    @GenericGenerator(
     name = "foreign", 
     strategy = "foreign", 
     parameters = { 
      @org.hibernate.annotations.Parameter(name = "property", value = "user") 
     }) 
    @Basic(optional = false) 
    @Column(name = "userid") 
    private Long userid; 

    @Basic(optional = false) 
    @NotNull 
    @Id 
    @Column(name = "lang") 
    private String lang; 

    @Basic(optional = false) 
    @NotNull 
    @Column(name = "level") 
    private int level; 

    @JoinColumn(
     name = "userid", 
     referencedColumnName = "id", 
     insertable = false, 
     updatable = false) 
    @ManyToOne(cascade = CascadeType.ALL, optional = false, fetch = FetchType.LAZY) 
    private User user; 

所以當我試圖從用戶對象更新列表UserLangs得到以下exepction:根據execption郎公司試圖

WARNING - Unexpected exception from beforeCompletion; transaction will roll back 

javax.persistence.PersistenceException: error during managed flush 
at org.hibernate.ejb.AbstractEntityManagerImpl$CallbackExceptionMapperImpl.mapManagedFlushFailure(AbstractEntityManagerImpl.java:1515) 
at org.hibernate.engine.transaction.synchronization.internal.SynchronizationCallbackCoordinatorImpl.beforeCompletion(SynchronizationCallbackCoordinatorImpl.java:117) 
at org.hibernate.engine.transaction.synchronization.internal.RegisteredSynchronization.beforeCompletion(RegisteredSynchronization.java:53) 
at org.apache.geronimo.transaction.manager.TransactionImpl.beforeCompletion(TransactionImpl.java:527) 
at org.apache.geronimo.transaction.manager.TransactionImpl.beforeCompletion(TransactionImpl.java:511) 
at org.apache.geronimo.transaction.manager.TransactionImpl.beforePrepare(TransactionImpl.java:413) 
at org.apache.geronimo.transaction.manager.TransactionImpl.commit(TransactionImpl.java:262) 
at org.apache.geronimo.transaction.manager.TransactionManagerImpl.commit(TransactionManagerImpl.java:252) 
at org.apache.openejb.core.transaction.JtaTransactionPolicy.completeTransaction(JtaTransactionPolicy.java:328) 
at org.apache.openejb.core.transaction.TxRequired.commit(TxRequired.java:75) 
at org.apache.openejb.core.transaction.EjbTransactionUtil.afterInvoke(EjbTransactionUtil.java:73) 
at org.apache.openejb.core.stateless.StatelessContainer._invoke(StatelessContainer.java:257) 
at org.apache.openejb.core.stateless.StatelessContainer.invoke(StatelessContainer.java:189) 
at org.apache.openejb.core.ivm.EjbObjectProxyHandler.synchronizedBusinessMethod(EjbObjectProxyHandler.java:253) 
at org.apache.openejb.core.ivm.EjbObjectProxyHandler.businessMethod(EjbObjectProxyHandler.java:248) 
at org.apache.openejb.core.ivm.EjbObjectProxyHandler._invoke(EjbObjectProxyHandler.java:92) 
at org.apache.openejb.core.ivm.BaseEjbProxyHandler.invoke(BaseEjbProxyHandler.java:279) 
at com.polyglong.dao.UsersDAO$LocalBeanProxy.updateUser(com/polyglong/dao/UsersDAO.java) 
at com.polyglong.dao.UserDAOTest.getUpdateUser(UserDAOTest.java:78) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
at java.lang.reflect.Method.invoke(Method.java:597) 
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) 
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) 
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) 
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) 
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) 
at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79) 
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71) 
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49) 
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) 
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) 
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) 
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) 
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) 
at org.junit.runners.ParentRunner.run(ParentRunner.java:236) 
at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:45) 
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:123) 
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:104) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
at java.lang.reflect.Method.invoke(Method.java:597) 
at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:164) 
at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:110) 
at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:175) 
at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWhenForked(SurefireStarter.java:107) 
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:68) 
    Caused by: javax.validation.ConstraintViolationException: Validation failed for classes [com.polyglong.entities.user.UserLangs] during persist time for groups [javax.validation.groups.Default, ] 
     List of constraint violations:[ 
ConstraintViolationImpl{rootBean=com.polyglong.entities.UserLangs[ userid=4,lang=null,level=0 ], propertyPath='lang', message='may not be null', leafBean=com.polyglong.entities.UserLangs[ userid=4,lang=null,level=0 ], value=null} 
    ] 
at  org.hibernate.cfg.beanvalidation.BeanValidationEventListener.validate(BeanValidationEventLi stener.java:159) 
at  org.hibernate.cfg.beanvalidation.BeanValidationEventListener.onPreInsert(BeanValidationEventListener.java:94) 
at  org.hibernate.action.internal.EntityInsertAction.preInsert(EntityInsertAction.java:181) 
at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:81) 
at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:362) 
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:354) 
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:275) 
at  org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:328) 
at     org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:52) 
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1233) 
at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:403) 
at    org.hibernate.engine.transaction.synchronization.internal.SynchronizationCallbackCoordinatorImpl.beforeCompletion(SynchronizationCallbackCoordinatorImpl.java:112) 
... 47 more 

被設置爲null,但它是奇怪的部分,因爲它的所有設置都是正確的,並且對象中沒有null,但是由於某種原因,它在提交後回滾。

當嘗試更新對象用戶時,合併工作正常,但是當我嘗試更新其內部的userlangs列表失敗時。

感謝

回答

3

您標誌着郎財產@NotNull,但你試圖保存一個爲空。你得到的錯誤是一個約束違反這樣說。

+0

但多數民衆贊成的事情,我不想保存任何爲空。 它設置了一個值,即使錯誤表示它爲空。 我做了一個調試,它有適當的值,直到合併。 – 2013-05-06 14:02:24

+0

你可以檢查所有實體嗎?如果您正在使用IDE,可能會在異常中設置一箇中斷,查看哪個實體正在緩衝?由於您有對其他實體w/CascadeType.ALL集的引用,因此您可能會收到保存此實體的錯誤,但問題在其他地方。 – CodeChimp 2013-05-06 16:41:56

+0

是的,我試過這樣做,但它沒有進入捕獲。 它看起來像是一次成功的合併,並且突然回滾 – 2013-05-07 15:49:21