Hibernate ConstraintViolationException

  • Themenstarter Themenstarter Gast
  • Beginndatum Beginndatum
Status
Nicht offen für weitere Antworten.
G

Gast

Gast
Hallo,

Umgebung:

Hibernate 3.2.4
HSQL
Spring 2.0.6
Java 1.6

Das Problem:

Klasse ContentObject hat 1 to many zu Asset (Assets) und weitere verschiedene 1 zu 1 Beziehungen zu Asset.

Der Fehler tritt auf, wenn ich versuche das ContentObject zu speichern und zwar nur dann wenn die 1:N (Assets) benutzt wird.

Das Mapping der 1:N funktioniert ohne Probleme (Anhand eines einfachen Bsp. getestet).

hier der Mappingcode

Code:
<set name="assets" lazy="true" access="field" cascade="save-update">
      <key>
        <column name="Assets" not-null="false" />
      </key>
      <one-to-many class="com.scoyo.data.content.AssetImpl" />
</set>

Der Ablauf des Codes:
1. Erstellen der verschiedenen Asset incl. save
2. Erstellen des ContentObject incl. setzen der AssetSet.
3. Speichern ContentObject --> Fehler

Meine Frage:

Kann mir jemand die Warning erklaeren. (Goggle hat leider nicht weitergeholfen) oder irgendwelche anderen Tipps geben.

ps:
Das der Fehler daraus resultiert das die referenzielle Integritaet verletzt ist; ist mir klar. Die Frage ist nur wieso. Alle Objecte werden gespeichert und selbst wenn nicht sollte es doch durch die cascade automatisch passieren...

Code:
Hibernate: update Asset set Assets=? where AssetId=?
WARN - SQL Error: -177, SQLState: 23000
ERROR - Integrity constraint violation - no parent FK3C9FAD0C8690287 table: ASSET in statement [update Asset set Assets=? where AssetId=?]
ERROR - Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: could not insert collection: [com.scoyo.data.content.ContentObjectImpl.assets#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.recreate(AbstractCollectionPersister.java:1183)
	at org.hibernate.action.CollectionRecreateAction.execute(CollectionRecreateAction.java:26)
	at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:250)
	at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:234)
	at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:145)
	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.springframework.orm.hibernate3.HibernateAccessor.flushIfNecessary(HibernateAccessor.java:390)
	at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:373)
	at org.springframework.orm.hibernate3.HibernateTemplate.save(HibernateTemplate.java:632)
	at com.scoyo.util.EntityDAOImpl.addEntity(EntityDAOImpl.java:14)
	at com.scoyo.service.LCMSService.registerEntity(LCMSService.java:46)
	at com.scoyo.service.LCMSService.registerLearnContentObject(LCMSService.java:119)
	at com.scoyo.util.Converter.transform(Converter.java:54)
	at SimpleTest.main(SimpleTest.java:50)
	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 com.intellij.rt.execution.application.AppMain.main(AppMain.java:90)
Caused by: java.sql.SQLException: Integrity constraint violation - no parent FK3C9FAD0C8690287 table: ASSET in statement [update Asset set Assets=? where AssetId=?]
	at org.hsqldb.jdbc.Util.throwError(Unknown Source)
	at org.hsqldb.jdbc.jdbcPreparedStatement.executeUpdate(Unknown Source)
	at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:102)
	at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:23)
	at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1146)
	... 20 more
Exception in thread "main" org.springframework.dao.DataIntegrityViolationException: could not insert collection: [com.scoyo.data.content.ContentObjectImpl.assets#1]; nested exception is org.hibernate.exception.ConstraintViolationException: could not insert collection: [com.scoyo.data.content.ContentObjectImpl.assets#1]
Caused by: org.hibernate.exception.ConstraintViolationException: could not insert collection: [com.scoyo.data.content.ContentObjectImpl.assets#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.recreate(AbstractCollectionPersister.java:1183)
	at org.hibernate.action.CollectionRecreateAction.execute(CollectionRecreateAction.java:26)
	at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:250)
	at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:234)
	at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:145)
	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.springframework.orm.hibernate3.HibernateAccessor.flushIfNecessary(HibernateAccessor.java:390)
	at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:373)
	at org.springframework.orm.hibernate3.HibernateTemplate.save(HibernateTemplate.java:632)
	at com.scoyo.util.EntityDAOImpl.addEntity(EntityDAOImpl.java:14)
	at com.scoyo.service.LCMSService.registerEntity(LCMSService.java:46)
	at com.scoyo.service.LCMSService.registerLearnContentObject(LCMSService.java:119)
	at com.scoyo.util.Converter.transform(Converter.java:54)
	at SimpleTest.main(SimpleTest.java:50)
	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 com.intellij.rt.execution.application.AppMain.main(AppMain.java:90)
Caused by: java.sql.SQLException: Integrity constraint violation - no parent FK3C9FAD0C8690287 table: ASSET in statement [update Asset set Assets=? where AssetId=?]
	at org.hsqldb.jdbc.Util.throwError(Unknown Source)
	at org.hsqldb.jdbc.jdbcPreparedStatement.executeUpdate(Unknown Source)
	at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:102)
	at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:23)
	at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1146)
	... 20 more

Process finished with exit code 1
 
Handelt es sich um eine Bidirektionale Verbindung?
Ist im Kind-Mapping inverse=true gesetzt?
Der Ablauf des Codes ist mir noch nicht ganz klar.
Wo genau werden den Assets die Referenz auf ContentObject zugewiesen?

Was anderes was mir noch aufgefallen ist:
Wenn cascading auf "save-update" gesetzt ist bleiben beim löschen des Parent die Kinder erhalten, die Referenz der Kinder wird aber aktualisiert => auf null gesetzt => Referenzielle Integrität verletzt. Damit es "vollautomatisch" (speichern/löschen in der DB durch hinzufügen/entfernen in der Collection) passiert sollte es auf "all-delete-orphan" stehen.

ms
 
Hallo,

hab den Fehler gefunden.

Eigentlich war es genau das was die Fehlermeldung auch gesagt hat.

In der Spalte Assets der Tabelle Asset gab es eine ConstraintViolation.

Das Problem ist das der jenige der das Modell erstellt hat (nicht ich) verschiedene Entities mit der Tabelle Asset verknuepft hat und jedes mal, weil es ja so schoen passt die "RealtionsSpalte" assets genannt.

--> Folglich hatte Sie mehrere KeyConstraint --> Das fuehrte dann zum Fehler.

nach gut 3 Stunden Suche...

Fazit (leider wieder mal bestaetigt):

Hibernate ist KEIN Ersatz fuer sauberen DB Entwurf...

und die Fehlermeldungen ernst nehmen ... 😳
 
Status
Nicht offen für weitere Antworten.

Neue Themen


Zurück
Oben