| Problem
Basically i have a One to Many relationship that works fine for create and add childs, but when i try to remove childs it throws this error, i'm using JPA Hibernate implementation.
 
 Hibernate version:
 Hibernate-Version: 3.2.6.ga
 Product: Hibernate EntityManager Version: 3.3.1.GA
 Spring-Version: 2.0.8
 
 Mapping documents:
 Coupon(One Side)
 ...
 @Entity
 @Table(name = "COUP_DTL")
 public class Coupon implements Serializable {
 
 /**
 *
 */
 ...
 @OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY, targetEntity = com.heb.coupon.model.Upc.class)
 @Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
 @JoinColumn(name = "COUP_ID", nullable = true)
 private List<Upc> products;
 
 Upc(Many side)
 @Entity
 @Table(name = "COUP_UPC")
 @AttributeOverrides( {
 @AttributeOverride(name = "coupId", column = @Column(name = "COUP_ID")),
 @AttributeOverride(name = "retailUnitGtin", column = @Column(name = "RETL_UNTI_GTIN")) })
 public class Upc implements Serializable {
 
 /**
 *
 */
 @EmbeddedId
 @ManyToOne
 private CouponUpcCompositeId id;
 @Column(name = "BUY_ITM_SW")
 private String buyItemSw;
 @Column(name = "CENT_OFF_SW")
 private String centOffSw;
 @Column(name = "DEPT_ID")
 private Integer deptId;
 ...
 
 CompositeUpcCompositeId
 ...
 private Integer coupId;
 private BigDecimal retailUnitGtin;
 
 public CouponUpcCompositeId(Integer coupId, BigDecimal retailUnitGtin) {
 super();
 this.coupId = coupId;
 this.retailUnitGtin = retailUnitGtin;
 }
 ...
 
 Code executed(JUnit)
 public void testUpdate() {
 try {
 
 dummyCouponCreate2 = couponService.getCoupon(1);
 
 Upc prod = dummyCouponCreate2.getProducts().iterator().next();
 
 prod.setId(null);
 dummyCouponCreate2.getProducts().remove(prod);
 
 Coupon result = couponService.update(dummyCouponCreate2);
 if (result != null) {
 assertTrue(true);
 } else {
 fail("Error: Coupon is null");
 }
 } catch (ServiceLookUpException e) {
 e.printStackTrace();
 fail("Error:" + e.getMessage());
 }
 }
 
 
 Service Implementation
 @Transactional
 public Coupon update(Coupon coupon) throws ServiceLookUpException {
 Coupon result = null;
 try {
 result = getJpaTemplate().merge(coupon);
 getJpaTemplate().flush();
 } catch (DataAccessException e) {
 throw new ServiceLookUpException(e);
 }
 return result;
 }
 
 Code between sessionFactory.openSession() and session.close():
 Using JPA Template, no session.
 
 Full stack trace of any exception that occurs:
 ERROR [main] org.hibernate.util.JDBCExceptionReporter - Cannot insert the value NULL into column 'COUP_ID', table 'hebcoupdev.dbo.COUP_UPC'; column does not allow nulls. UPDATE fails.
 ERROR [main] org.hibernate.event.def.AbstractFlushingEventListener - Could not synchronize database state with session
 org.hibernate.exception.ConstraintViolationException: could not delete collection rows: [com.heb.coupon.model.Coupon.products#1]
 at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
 at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
 at org.hibernate.persister.collection.AbstractCollectionPersister.deleteRows(AbstractCollectionPersister.java:1292)
 at org.hibernate.action.CollectionUpdateAction.execute(CollectionUpdateAction.java:64)
 at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
 at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:263)
 at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:170)
 at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
 at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
 at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
 at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:297)
 at org.springframework.orm.jpa.JpaTemplate$8.doInJpa(JpaTemplate.java:283)
 at org.springframework.orm.jpa.JpaTemplate.execute(JpaTemplate.java:185)
 at org.springframework.orm.jpa.JpaTemplate.flush(JpaTemplate.java:281)
 at com.heb.coupon.service.CouponServiceImpl.update(CouponServiceImpl.java:254)
 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:585)
 at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:304)
 at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
 at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
 at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
 at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
 at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
 at $Proxy23.update(Unknown Source)
 at com.heb.coupon.test.CouponServiceTest.testUpdate(CouponServiceTest.java:207)
 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:585)
 at junit.framework.TestCase.runTest(TestCase.java:164)
 at junit.framework.TestCase.runBare(TestCase.java:130)
 at org.springframework.test.ConditionalTestCase.runBare(ConditionalTestCase.java:69)
 at org.springframework.test.annotation.AbstractAnnotationAwareTransactionalTests.access$001(AbstractAnnotationAwareTransactionalTests.java:47)
 at org.springframework.test.annotation.AbstractAnnotationAwareTransactionalTests$1.run(AbstractAnnotationAwareTransactionalTests.java:113)
 at org.springframework.test.annotation.AbstractAnnotationAwareTransactionalTests.runTest(AbstractAnnotationAwareTransactionalTests.java:176)
 at org.springframework.test.annotation.AbstractAnnotationAwareTransactionalTests.runTestTimed(AbstractAnnotationAwareTransactionalTests.java:150)
 at org.springframework.test.annotation.AbstractAnnotationAwareTransactionalTests.runBare(AbstractAnnotationAwareTransactionalTests.java:109)
 at org.springframework.test.jpa.AbstractJpaTests.runBare(AbstractJpaTests.java:174)
 at org.springframework.test.jpa.AbstractJpaTests.runBare(AbstractJpaTests.java:254)
 at junit.framework.TestResult$1.protect(TestResult.java:106)
 at junit.framework.TestResult.runProtected(TestResult.java:124)
 at junit.framework.TestResult.run(TestResult.java:109)
 at junit.framework.TestCase.run(TestCase.java:120)
 at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
 at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
 Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Cannot insert the value NULL into column 'COUP_ID', table 'hebcoupdev.dbo.COUP_UPC'; column does not allow nulls. UPDATE fails.
 at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(Unknown Source)
 at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(Unknown Source)
 at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(Unknown Source)
 at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(Unknown Source)
 at com.microsoft.sqlserver.jdbc.TDSCommand.execute(Unknown Source)
 at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(Unknown Source)
 at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(Unknown Source)
 at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(Unknown Source)
 at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeUpdate(Unknown Source)
 at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:23)
 at org.hibernate.persister.collection.AbstractCollectionPersister.deleteRows(AbstractCollectionPersister.java:1261)
 ... 48 more
 DEBUG [main] org.springframework.transaction.interceptor.TransactionInterceptor - Completing transaction for [com.heb.coupon.service.CouponService.update] after exception: com.heb.coupon.exceptions.ServiceLookUpException: org.springframework.dao.DataIntegrityViolationException: org.hibernate.exception.ConstraintViolationException: could not delete collection rows: [com.heb.coupon.model.Coupon.products#1]; nested exception is javax.persistence.EntityExistsException: org.hibernate.exception.ConstraintViolationException: could not delete collection rows: [com.heb.coupon.model.Coupon.products#1]
 DEBUG [main] org.springframework.transaction.interceptor.RuleBasedTransactionAttribute - Applying rules to determine whether transaction should rollback on com.heb.coupon.exceptions.ServiceLookUpException: org.springframework.dao.DataIntegrityViolationException: org.hibernate.exception.ConstraintViolationException: could not delete collection rows: [com.heb.coupon.model.Coupon.products#1]; nested exception is javax.persistence.EntityExistsException: org.hibernate.exception.ConstraintViolationException: could not delete collection rows: [com.heb.coupon.model.Coupon.products#1]
 DEBUG [main] org.springframework.transaction.interceptor.RuleBasedTransactionAttribute - Winning rollback rule is: null
 DEBUG [main] org.springframework.transaction.interceptor.RuleBasedTransactionAttribute - No relevant rollback rule found: applying superclass default
 com.heb.coupon.exceptions.ServiceLookUpException: org.springframework.dao.DataIntegrityViolationException: org.hibernate.exception.ConstraintViolationException: could not delete collection rows: [com.heb.coupon.model.Coupon.products#1]; nested exception is javax.persistence.EntityExistsException: org.hibernate.exception.ConstraintViolationException: could not delete collection rows: [com.heb.coupon.model.Coupon.products#1]
 at com.heb.coupon.service.CouponServiceImpl.update(CouponServiceImpl.java:256)
 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:585)
 at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:304)
 at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
 at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
 at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
 at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
 at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
 at $Proxy23.update(Unknown Source)
 at com.heb.coupon.test.CouponServiceTest.testUpdate(CouponServiceTest.java:207)
 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:585)
 at junit.framework.TestCase.runTest(TestCase.java:164)
 at junit.framework.TestCase.runBare(TestCase.java:130)
 at org.springframework.test.ConditionalTestCase.runBare(ConditionalTestCase.java:69)
 at org.springframework.test.annotation.AbstractAnnotationAwareTransactionalTests.access$001(AbstractAnnotationAwareTransactionalTests.java:47)
 at org.springframework.test.annotation.AbstractAnnotationAwareTransactionalTests$1.run(AbstractAnnotationAwareTransactionalTests.java:113)
 at org.springframework.test.annotation.AbstractAnnotationAwareTransactionalTests.runTest(AbstractAnnotationAwareTransactionalTests.java:176)
 at org.springframework.test.annotation.AbstractAnnotationAwareTransactionalTests.runTestTimed(AbstractAnnotationAwareTransactionalTests.java:150)
 at org.springframework.test.annotation.AbstractAnnotationAwareTransactionalTests.runBare(AbstractAnnotationAwareTransactionalTests.java:109)
 at org.springframework.test.jpa.AbstractJpaTests.runBare(AbstractJpaTests.java:174)
 at org.springframework.test.jpa.AbstractJpaTests.runBare(AbstractJpaTests.java:254)
 at junit.framework.TestResult$1.protect(TestResult.java:106)
 at junit.framework.TestResult.runProtected(TestResult.java:124)
 at junit.framework.TestResult.run(TestResult.java:109)
 at junit.framework.TestCase.run(TestCase.java:120)
 at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
 at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
 Caused by: org.springframework.dao.DataIntegrityViolationException: org.hibernate.exception.ConstraintViolationException: could not delete collection rows: [com.heb.coupon.model.Coupon.products#1]; nested exception is javax.persistence.EntityExistsException: org.hibernate.exception.ConstraintViolationException: could not delete collection rows: [com.heb.coupon.model.Coupon.products#1]
 at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:246)
 at org.springframework.orm.jpa.DefaultJpaDialect.translateExceptionIfPossible(DefaultJpaDialect.java:114)
 at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:212)
 at org.springframework.orm.jpa.JpaAccessor.translateIfNecessary(JpaAccessor.java:152)
 at org.springframework.orm.jpa.JpaTemplate.execute(JpaTemplate.java:190)
 at org.springframework.orm.jpa.JpaTemplate.flush(JpaTemplate.java:281)
 at com.heb.coupon.service.CouponServiceImpl.update(CouponServiceImpl.java:254)
 ... 36 more
 Caused by: javax.persistence.EntityExistsException: org.hibernate.exception.ConstraintViolationException: could not delete collection rows: [com.heb.coupon.model.Coupon.products#1]
 at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:605)
 at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:300)
 at org.springframework.orm.jpa.JpaTemplate$8.doInJpa(JpaTemplate.java:283)
 at org.springframework.orm.jpa.JpaTemplate.execute(JpaTemplate.java:185)
 ... 38 more
 Caused by: org.hibernate.exception.ConstraintViolationException: could not delete collection rows: [com.heb.coupon.model.Coupon.products#1]
 at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
 at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
 at org.hibernate.persister.collection.AbstractCollectionPersister.deleteRows(AbstractCollectionPersister.java:1292)
 at org.hibernate.action.CollectionUpdateAction.execute(CollectionUpdateAction.java:64)
 at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
 at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:263)
 at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:170)
 at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
 at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
 at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
 at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:297)
 ... 40 more
 Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Cannot insert the value NULL into column 'COUP_ID', table 'hebcoupdev.dbo.COUP_UPC'; column does not allow nulls. UPDATE fails.
 at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(Unknown Source)
 at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(Unknown Source)
 at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(Unknown Source)
 at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(Unknown Source)
 at com.microsoft.sqlserver.jdbc.TDSCommand.execute(Unknown Source)
 at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(Unknown Source)
 at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(Unknown Source)
 at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(Unknown Source)
 at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeUpdate(Unknown Source)
 at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:23)
 at org.hibernate.persister.collection.AbstractCollectionPersister.deleteRows(AbstractCollectionPersister.java:1261)
 ... 48 more
 Name and version of the database you are using:
 SQL Server 2005 Name: hebcoupdev
 The generated SQL (show_sql=true):
 Hibernate: select coupon0_.COUP_ID as COUP1_1_0_, coupon0_.AD_EFF_DT as AD2_1_0_, coupon0_.AD_USR_ID as AD3_1_0_, coupon0_.BDM_USR_ID as BDM4_1_0_, coupon0_.COUP_BAR_CD_CKDG as COUP5_1_0_, coupon0_.COUP_BAR_CD_1_NBR as COUP6_1_0_, coupon0_.COUP_BAR_CD_PRE_DGT_NBR as COUP7_1_0_, coupon0_.COUP_BAR_CD_2_NBR as COUP8_1_0_, coupon0_.COUP_BAR_CD_3_NBR as COUP9_1_0_, coupon0_.CORP_DLVR_SW as CORP10_1_0_, coupon0_.AD_EVNT_INCL_IN_TYP_CD as AD11_1_0_, coupon0_.ART_CD_SW as ART12_1_0_, coupon0_.ART_CD as ART13_1_0_, coupon0_.BDM_CD as BDM14_1_0_, coupon0_.COUP_DEAL_ID as COUP15_1_0_, coupon0_.COUP_DES as COUP16_1_0_, coupon0_.COUP_EXPRN_DT as COUP17_1_0_, coupon0_.FAM_CD_BAR_CD as FAM18_1_0_, coupon0_.COUP_ISU_DT as COUP19_1_0_, coupon0_.MAX_COUP_VAL_AMT as MAX20_1_0_, coupon0_.COUP_OFR_DEAL_SW as COUP21_1_0_, coupon0_.COUP_SZ_CD as COUP22_1_0_, coupon0_.COUP_STAT_TS as COUP23_1_0_, coupon0_.COUP_STAT_UPDT_UID as COUP24_1_0_, coupon0_.COUP_STAT_CD as COUP25_1_0_, coupon0_.STR_SPEC_SW as STR26_1_0_, coupon0_.TPR_RQSTD_SW as TPR27_1_0_, coupon0_.TRP_RQSTD_ON_DT as TRP28_1_0_, coupon0_.DSIR_TPR_RETL_AMT as DSIR29_1_0_, coupon0_.COUP_TYP_CD as COUP30_1_0_, coupon0_.VEND_ACPT_UFD_INIT_TXT as VEND31_1_0_, coupon0_.UNIV_COUP_CD_SW as UNIV32_1_0_, coupon0_.COUP_VAL_AMT as COUP33_1_0_, coupon0_.COUP_VAL_CD as COUP34_1_0_, coupon0_.INRNET_USE_SW as INRNET35_1_0_, coupon0_.OFR_SUBMTD_DT as OFR36_1_0_, coupon0_.RELATED_COUP_DEAL_ID as RELATED37_1_0_, coupon0_.SPCL_INSTN_SW as SPCL38_1_0_, coupon0_.VEND_USR_ID as VEND39_1_0_, coupon0_.VEND_LDAP_ORG_ID as VEND40_1_0_ from dbo.COUP_DTL coupon0_ where coupon0_.COUP_ID=?
 Hibernate: select products0_.COUP_ID as COUP1_1_, products0_.RETL_UNTI_GTIN as RETL2_1_, products0_.COUP_ID as COUP1_7_0_, products0_.RETL_UNTI_GTIN as RETL2_7_0_, products0_.BUY_ITM_SW as BUY3_7_0_, products0_.CENT_OFF_SW as CENT4_7_0_, products0_.DEPT_ID as DEPT5_7_0_ from dbo.COUP_UPC products0_ where products0_.COUP_ID=?
 ...
 Hibernate: update dbo.COUP_UPC set COUP_ID=null where COUP_ID=? and COUP_ID=? and RETL_UNTI_GTIN=?
 
 Problems with Session and transaction handling?
 No
 _________________
 JEGC
 
 
 |