org.hibernate.exception.ConstraintViolationException: could not insert: [com.plumtree.analytics.core.persist.Group]+

Oracle Community | 766487 | 7 years ago
  1. 0

    WCI Sync Issue

    Oracle Community | 7 years ago | 766487
    org.hibernate.exception.ConstraintViolationException: could not insert: [com.plumtree.analytics.core.persist.Group]+
  2. 0

    [HHH-616] replicate() with a bidirectional relationship fails (dom4j) - Hibernate JIRA

    atlassian.net | 1 year ago
    org.hibernate.exception.ConstraintViolationException: could not insert:
  3. 0

    Hibernate / Mailing Lists

    sourceforge.net | 11 months ago
    org.hibernate.exception.ConstraintViolationException: could not insert: [data.Foo]
  4. Speed up your debug routine!

    Automated exception search integrated into your IDE

  5. 0

    Here is the original forum post: http://forum.hibernate.org/viewtopic.php?t=943520 Basically, I am trying to transfer stuff from one DB to another via the Dom4j session. I create an XML document from the source DB and then I am using session.replicate() on the receiving end to instantiate the new objects from that XML document. It works for simple objects, but when I have a bidirectional reference, it is unable to .replicate() the pair of objects, even if I try to commit() them all in the same transaction. I have created a simple model to reproduce this. Mapping document: <hibernate-mapping package="data"> <class name="Foo" table="foo" > <id name="id" node="@id" type="string" unsaved-value="null" column="foo_id" > <generator class="uuid" /> </id> <property name="prop1" column="prop1" type="string" /> <many-to-one name="bar" column="bar_id" class="Bar" not-null="false" embed-xml="false" /> <property name="index" column="foo_index" type="int" update="true" /> </class> <class name="Bar" table="bar" > <id name="id" node="@id" type="string" unsaved-value="null" column="bar_id" > <generator class="uuid" /> </id> <list name="foos" node="foos" embed-xml="true" inverse="true" lazy="false"> <key column="bar_id"/> <list-index column="foo_index"/> <one-to-many class="Foo" embed-xml="false" node="foo" /> </list> </class> </hibernate-mapping> Reconstitution code: try { SAXReader reader = new SAXReader(); Document doc = reader.read(new File("content.xml")); Session session = currentSession(); Session s = session.getSession(EntityMode.DOM4J); Transaction txn = s.beginTransaction(); { List elems = doc.getRootElement().elements(); Iterator it = elems.iterator(); while(it.hasNext()) { Element e = (Element)it.next(); log.debug(e.getName()); if(e.getName().equals("Foo")) s.replicate("data.Foo",e, ReplicationMode.IGNORE); if(e.getName().equals("Bar")) s.replicate("data.Bar",e, ReplicationMode.IGNORE); } } txn.commit(); } catch(DocumentException e) { log.error(e); } Here is the stack trace: 150 [main] ERROR org.hibernate.util.JDBCExceptionReporter - Cannot add or update a child row: a foreign key constraint fails 291 [main] ERROR org.hibernate.event.def.AbstractFlushingEventListener - Could not synchronize database state with session org.hibernate.exception.ConstraintViolationException: could not insert: [data.Foo] at org.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConverter.java:74) at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43) at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:1869) at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:2200) at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:46) 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:136) 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:329) at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:86) at Test.reconstitute(Test.java:69) at Test.main(Test.java:40) 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 com.intellij.rt.execution.application.AppMain.main(AppMain.java:78) Caused by: java.sql.SQLException: Cannot add or update a child row: a foreign key constraint fails at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2851) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1531) at com.mysql.jdbc.ServerPreparedStatement.serverExecute(ServerPreparedStatement.java:1366) at com.mysql.jdbc.ServerPreparedStatement.executeInternal(ServerPreparedStatement.java:952) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1974) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1897) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1758) at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:22) at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:1853) ... 17 more Exception in thread "main" org.hibernate.exception.ConstraintViolationException: could not insert: [data.Foo] at org.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConverter.java:74) at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43) at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:1869) at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:2200) at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:46) 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:136) 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:329) at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:86) at Test.reconstitute(Test.java:69) at Test.main(Test.java:40) And finally, here is the XML document I am attempting to reconstitute: <root> <Foo id="402881e4045ce90c01045ce90f950002"> <prop1>abcd</prop1> <bar>402881e4045ce90c01045ce90f770001</bar> <index>0</index> </Foo> <Foo id="402881e4045ce90c01045ce90f9f0003"> <prop1>456</prop1> <bar>402881e4045ce90c01045ce90f770001</bar> <index>1</index> </Foo> <Bar id="402881e4045ce90c01045ce90f770001"> <foos> <foo>402881e4045ce90c01045ce90f950002</foo> <foo>402881e4045ce90c01045ce90f9f0003</foo> </foos> </Bar> </root>

    Hibernate JIRA | 1 decade ago | David Tashima
    org.hibernate.exception.ConstraintViolationException: could not insert: [data.Foo]
  6. 0

    Here is the original forum post: http://forum.hibernate.org/viewtopic.php?t=943520 Basically, I am trying to transfer stuff from one DB to another via the Dom4j session. I create an XML document from the source DB and then I am using session.replicate() on the receiving end to instantiate the new objects from that XML document. It works for simple objects, but when I have a bidirectional reference, it is unable to .replicate() the pair of objects, even if I try to commit() them all in the same transaction. I have created a simple model to reproduce this. Mapping document: <hibernate-mapping package="data"> <class name="Foo" table="foo" > <id name="id" node="@id" type="string" unsaved-value="null" column="foo_id" > <generator class="uuid" /> </id> <property name="prop1" column="prop1" type="string" /> <many-to-one name="bar" column="bar_id" class="Bar" not-null="false" embed-xml="false" /> <property name="index" column="foo_index" type="int" update="true" /> </class> <class name="Bar" table="bar" > <id name="id" node="@id" type="string" unsaved-value="null" column="bar_id" > <generator class="uuid" /> </id> <list name="foos" node="foos" embed-xml="true" inverse="true" lazy="false"> <key column="bar_id"/> <list-index column="foo_index"/> <one-to-many class="Foo" embed-xml="false" node="foo" /> </list> </class> </hibernate-mapping> Reconstitution code: try { SAXReader reader = new SAXReader(); Document doc = reader.read(new File("content.xml")); Session session = currentSession(); Session s = session.getSession(EntityMode.DOM4J); Transaction txn = s.beginTransaction(); { List elems = doc.getRootElement().elements(); Iterator it = elems.iterator(); while(it.hasNext()) { Element e = (Element)it.next(); log.debug(e.getName()); if(e.getName().equals("Foo")) s.replicate("data.Foo",e, ReplicationMode.IGNORE); if(e.getName().equals("Bar")) s.replicate("data.Bar",e, ReplicationMode.IGNORE); } } txn.commit(); } catch(DocumentException e) { log.error(e); } Here is the stack trace: 150 [main] ERROR org.hibernate.util.JDBCExceptionReporter - Cannot add or update a child row: a foreign key constraint fails 291 [main] ERROR org.hibernate.event.def.AbstractFlushingEventListener - Could not synchronize database state with session org.hibernate.exception.ConstraintViolationException: could not insert: [data.Foo] at org.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConverter.java:74) at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43) at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:1869) at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:2200) at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:46) 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:136) 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:329) at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:86) at Test.reconstitute(Test.java:69) at Test.main(Test.java:40) 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 com.intellij.rt.execution.application.AppMain.main(AppMain.java:78) Caused by: java.sql.SQLException: Cannot add or update a child row: a foreign key constraint fails at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2851) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1531) at com.mysql.jdbc.ServerPreparedStatement.serverExecute(ServerPreparedStatement.java:1366) at com.mysql.jdbc.ServerPreparedStatement.executeInternal(ServerPreparedStatement.java:952) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1974) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1897) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1758) at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:22) at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:1853) ... 17 more Exception in thread "main" org.hibernate.exception.ConstraintViolationException: could not insert: [data.Foo] at org.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConverter.java:74) at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43) at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:1869) at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:2200) at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:46) 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:136) 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:329) at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:86) at Test.reconstitute(Test.java:69) at Test.main(Test.java:40) And finally, here is the XML document I am attempting to reconstitute: <root> <Foo id="402881e4045ce90c01045ce90f950002"> <prop1>abcd</prop1> <bar>402881e4045ce90c01045ce90f770001</bar> <index>0</index> </Foo> <Foo id="402881e4045ce90c01045ce90f9f0003"> <prop1>456</prop1> <bar>402881e4045ce90c01045ce90f770001</bar> <index>1</index> </Foo> <Bar id="402881e4045ce90c01045ce90f770001"> <foos> <foo>402881e4045ce90c01045ce90f950002</foo> <foo>402881e4045ce90c01045ce90f9f0003</foo> </foos> </Bar> </root>

    Hibernate JIRA | 1 decade ago | David Tashima
    org.hibernate.exception.ConstraintViolationException: could not insert: [data.Foo]

    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. org.hibernate.exception.ConstraintViolationException

      could not insert: [com.plumtree.analytics.core.persist.Group]+

      at org.hibernate.exception.ErrorCodeConverter.convert()
    2. Hibernate
      BasicEntityPersister.insert
      1. org.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConverter.java:74)
      2. org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
      3. org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:1869)
      4. org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:2200)
      4 frames
    3. Hibernate
      EntityInsertAction.execute
      1. org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:46)
      1 frame
    4. 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:136)
      3 frames
    5. 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
    6. Hibernate
      JDBCTransaction.commit
      1. org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:86)
      1 frame
    7. com.plumtree.analytics
      RunJobs.main
      1. com.plumtree.analytics.job.AnalyticsJobBaseObject.commitTransaction(AnalyticsJobBaseObject.java:227)
      2. com.plumtree.analytics.job.GroupSyncJob.run(GroupSyncJob.java:152)
      3. com.plumtree.analytics.job.RunJobs.runJob(RunJobs.java:129)
      4. com.plumtree.analytics.job.RunJobs.main(RunJobs.java:305)
      4 frames