org.hibernate.exception.ConstraintViolationException: could not insert collection rows: [net.orless.hibernate.tests.A.b#a]

Hibernate JIRA | Aleksei Valikov | 1 decade ago
tip
Your exception is missing from the Samebug knowledge base.
Here are the best solutions we found on the Internet.
Click on the to mark the helpful solution and get rewards for you help.
  1. 0

    http://forum.hibernate.org/viewtopic.php?t=944753 My object has a list of primitive values (say, strings), where values could possibly be null. I'm getting a constraint violation when trying to change previously saved null to non-null. I've implemented the simplest possible test case that reproduces the problem. Test scenario is: Session 1 1. Set a.b[0] to null 2. Save or update. Session 2 3. Set a.b[0] to "b". 4. Save or update. 5. Enjoy an exception. I've traced a bit and it seems that in PersistentList.needsUpdating(...) you only update if old value was not null: return i<sn.size() && sn.get(i)!=null && list.get(i)!=null && elemType.isDirty( list.get(i), sn.get(i), getSession() ); Since I previously had null, the item is not updated but inserted instead. And I get the constraint violation. Is this a bug? Not critical, but quite annoying. Hibernate version: Hibernate 3.0.5 Mapping documents: <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="net.orless.hibernate.tests"> <class name="A" table="A"> <id name="id"/> <list name="b"> <key column="id"/> <list-index column="index"/> <element column="value" type="string" not-null="false"/> </list> </class> </hibernate-mapping> Unit test: public class A { private String id; private List b = new ArrayList(); public String getId() { return id; } public void setId(String id) { this.id = id; } public List getB() { return b; } public void setB(List b) { this.b = b; } } public class ATest extends TestCase { public ATest(String x) { super(x); } public void testSaveOrUpdate() throws HibernateException, SQLException { final A a = new A(); a.setId("a"); a.getB().add(null); Session s = openSession(); Transaction t = s.beginTransaction(); s.saveOrUpdate(a); t.commit(); s.close(); a.getB().set(0, "b"); s = openSession(); t = s.beginTransaction(); s.saveOrUpdate(a); t.commit(); s.close(); } protected String[] getMappings() { return new String[]{ "A.hbm.xml" }; } } Full stack trace of any exception that occurs: org.hibernate.exception.ConstraintViolationException: could not insert collection rows: [net.orless.hibernate.tests.A.b#a] at org.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConverter.java:74) at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43) at org.hibernate.persister.collection.AbstractCollectionPersister.insertRows(AbstractCollectionPersister.java:1087) at org.hibernate.action.CollectionUpdateAction.execute(CollectionUpdateAction.java:48) at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:239) at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:223) at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:139) at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:274) at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27) at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:730) at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:324) at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:86) at net.orless.hibernate.tests.ATest.testSaveOrUpdate(ATest.java:32) 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:154) at net.orless.hibernate.tests.TestCase.runTest(TestCase.java:129) at junit.framework.TestCase.runBare(TestCase.java:127) 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:118) at junit.framework.TestSuite.runTest(TestSuite.java:208) at junit.framework.TestSuite.run(TestSuite.java:203) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:474) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:342) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:194) Caused by: java.sql.SQLException: Unique constraint violation: in statement [insert into b (id, index, value) values (?, ?, ?)] at org.hsqldb.jdbc.jdbcUtil.throwError(jdbcUtil.java:62) at org.hsqldb.jdbc.jdbcPreparedStatement.executeUpdate(jdbcPreparedStatement.java:412) at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:22) at org.hibernate.persister.collection.AbstractCollectionPersister.insertRows(AbstractCollectionPersister.java:1073) ... 26 more Name and version of the database you are using: HSQL 1.7.3

    Hibernate JIRA | 1 decade ago | Aleksei Valikov
    org.hibernate.exception.ConstraintViolationException: could not insert collection rows: [net.orless.hibernate.tests.A.b#a]
  2. 0

    http://forum.hibernate.org/viewtopic.php?t=944753 My object has a list of primitive values (say, strings), where values could possibly be null. I'm getting a constraint violation when trying to change previously saved null to non-null. I've implemented the simplest possible test case that reproduces the problem. Test scenario is: Session 1 1. Set a.b[0] to null 2. Save or update. Session 2 3. Set a.b[0] to "b". 4. Save or update. 5. Enjoy an exception. I've traced a bit and it seems that in PersistentList.needsUpdating(...) you only update if old value was not null: return i<sn.size() && sn.get(i)!=null && list.get(i)!=null && elemType.isDirty( list.get(i), sn.get(i), getSession() ); Since I previously had null, the item is not updated but inserted instead. And I get the constraint violation. Is this a bug? Not critical, but quite annoying. Hibernate version: Hibernate 3.0.5 Mapping documents: <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="net.orless.hibernate.tests"> <class name="A" table="A"> <id name="id"/> <list name="b"> <key column="id"/> <list-index column="index"/> <element column="value" type="string" not-null="false"/> </list> </class> </hibernate-mapping> Unit test: public class A { private String id; private List b = new ArrayList(); public String getId() { return id; } public void setId(String id) { this.id = id; } public List getB() { return b; } public void setB(List b) { this.b = b; } } public class ATest extends TestCase { public ATest(String x) { super(x); } public void testSaveOrUpdate() throws HibernateException, SQLException { final A a = new A(); a.setId("a"); a.getB().add(null); Session s = openSession(); Transaction t = s.beginTransaction(); s.saveOrUpdate(a); t.commit(); s.close(); a.getB().set(0, "b"); s = openSession(); t = s.beginTransaction(); s.saveOrUpdate(a); t.commit(); s.close(); } protected String[] getMappings() { return new String[]{ "A.hbm.xml" }; } } Full stack trace of any exception that occurs: org.hibernate.exception.ConstraintViolationException: could not insert collection rows: [net.orless.hibernate.tests.A.b#a] at org.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConverter.java:74) at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43) at org.hibernate.persister.collection.AbstractCollectionPersister.insertRows(AbstractCollectionPersister.java:1087) at org.hibernate.action.CollectionUpdateAction.execute(CollectionUpdateAction.java:48) at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:239) at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:223) at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:139) at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:274) at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27) at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:730) at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:324) at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:86) at net.orless.hibernate.tests.ATest.testSaveOrUpdate(ATest.java:32) 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:154) at net.orless.hibernate.tests.TestCase.runTest(TestCase.java:129) at junit.framework.TestCase.runBare(TestCase.java:127) 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:118) at junit.framework.TestSuite.runTest(TestSuite.java:208) at junit.framework.TestSuite.run(TestSuite.java:203) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:474) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:342) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:194) Caused by: java.sql.SQLException: Unique constraint violation: in statement [insert into b (id, index, value) values (?, ?, ?)] at org.hsqldb.jdbc.jdbcUtil.throwError(jdbcUtil.java:62) at org.hsqldb.jdbc.jdbcPreparedStatement.executeUpdate(jdbcPreparedStatement.java:412) at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:22) at org.hibernate.persister.collection.AbstractCollectionPersister.insertRows(AbstractCollectionPersister.java:1073) ... 26 more Name and version of the database you are using: HSQL 1.7.3

    Hibernate JIRA | 1 decade ago | Aleksei Valikov
    org.hibernate.exception.ConstraintViolationException: could not insert collection rows: [net.orless.hibernate.tests.A.b#a]
  3. 0

    Error while loading Metadata ontp Planning through ODI

    Oracle Community | 7 years ago | 713868
    java.sql.SQLException: Table not found: C$_0Account in statement [drop table "C$_0Account"]
  4. Speed up your debug routine!

    Automated exception search integrated into your IDE

  5. 0

    Help Required

    Oracle Community | 7 years ago | Varun Kaushal
    java.sql.SQLException: Wrong data type: in statement [create table "C$_0File" ( )]
  6. 0

    Getting error when i am executing the mapping(load the data into planning )

    Oracle Community | 7 years ago | 769224
    java.sql.SQLException: Table not found: C$_0Entity in statement [drop table "C$_0Entity"]

    1 unregistered visitors
    Not finding the right solution?
    Take a tour to get the most out of Samebug.

    Tired of useless tips?

    Automated exception search integrated into your IDE

    Root Cause Analysis

    1. java.sql.SQLException

      Unique constraint violation: in statement [insert into b (id, index, value) values (?, ?, ?)]

      at org.hsqldb.jdbc.jdbcUtil.throwError()
    2. HSQLDB
      jdbcPreparedStatement.executeUpdate
      1. org.hsqldb.jdbc.jdbcUtil.throwError(jdbcUtil.java:62)
      2. org.hsqldb.jdbc.jdbcPreparedStatement.executeUpdate(jdbcPreparedStatement.java:412)
      2 frames
    3. Hibernate
      AbstractCollectionPersister.insertRows
      1. org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:22)
      2. org.hibernate.persister.collection.AbstractCollectionPersister.insertRows(AbstractCollectionPersister.java:1073)
      2 frames
    4. Hibernate
      CollectionUpdateAction.execute
      1. org.hibernate.action.CollectionUpdateAction.execute(CollectionUpdateAction.java:48)
      1 frame
    5. Hibernate
      ActionQueue.executeActions
      1. org.hibernate.engine.ActionQueue.execute(ActionQueue.java:239)
      2. org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:223)
      3. org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:139)
      3 frames
    6. Hibernate
      SessionImpl.managedFlush
      1. org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:274)
      2. org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
      3. org.hibernate.impl.SessionImpl.flush(SessionImpl.java:730)
      4. org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:324)
      4 frames
    7. Hibernate
      JDBCTransaction.commit
      1. org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:86)
      1 frame
    8. net.orless.hibernate
      ATest.testSaveOrUpdate
      1. net.orless.hibernate.tests.ATest.testSaveOrUpdate(ATest.java:32)
      1 frame
    9. Java RT
      Method.invoke
      1. sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      2. sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      3. sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      4. java.lang.reflect.Method.invoke(Method.java:585)
      4 frames
    10. JUnit
      TestCase.runTest
      1. junit.framework.TestCase.runTest(TestCase.java:154)
      1 frame
    11. net.orless.hibernate
      TestCase.runTest
      1. net.orless.hibernate.tests.TestCase.runTest(TestCase.java:129)
      1 frame
    12. JUnit
      TestSuite.run
      1. junit.framework.TestCase.runBare(TestCase.java:127)
      2. junit.framework.TestResult$1.protect(TestResult.java:106)
      3. junit.framework.TestResult.runProtected(TestResult.java:124)
      4. junit.framework.TestResult.run(TestResult.java:109)
      5. junit.framework.TestCase.run(TestCase.java:118)
      6. junit.framework.TestSuite.runTest(TestSuite.java:208)
      7. junit.framework.TestSuite.run(TestSuite.java:203)
      7 frames
    13. JUnit3 Runner
      RemoteTestRunner.main
      1. org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:474)
      2. org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:342)
      3. org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:194)
      3 frames