Pb Transaction sur ConstraintViolationException
Bonjour,
Je tente de faire une insertion d'un element dans une table dépendant d'une table pére( contrainte foreign key).
Pour des raisons particulières je n'ai pas mapper cette relation dans les fichiers de config.
Je fais donc appel à une "named query" qui remplie la table parent puis je sauvegarde mon objet, le tout dans une transaction.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
Session session = null;
Transaction tx = null;
try {
session = factory.openSession();
tx = session.beginTransaction();
MyObjet myObjet = new MyObjet();
/* Etape 1 */
Query query = session.getNamedQuery("test");
query.setString("id", "TEST_001");
query.executeUpdate(); //OK pas d'erreur
/* Etape 2 */
session.save(critere);// erreur : org.hibernate.exception.ConstraintViolationException
tx.commit();
} catch (Exception e) {
if (tx != null) tx.rollback();
e.printStackTrace();
} finally {
session.close();
} |
Je suis en JDBC direct sans JTA.
Etant en théorie dans la même transaction ca ne devrait pas poser de problème, mais j'ai le message suivant :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
Hibernate: INSERT INTO TAB1 (ID_TAB1,TYPE)
VALUES(?,'XXX')
numLineChanged : 1
Hibernate: insert into TAB2 (COL1, COL2, ID_TAB1) values (?, ?, ?)
org.hibernate.util.JDBCExceptionReporter logExceptions
ATTENTION: SQL Error: 2291, SQLState: 23000
org.hibernate.util.JDBCExceptionReporter logExceptions
GRAVE: ORA-02291: violation de contrainte d'integrite (TEST.FK_LIEN_TAB2_TAB1) - cle parent introuvable
org.hibernate.event.def.AbstractFlushingEventListener performExecutions
GRAVE: Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:249)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:235)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:139)
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.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
...
Caused by: java.sql.BatchUpdateException: ORA-02291: violation de contrainte d'integrite (TEST.FK_LIEN_TAB2_TAB1) - cle parent introuvable
at oracle.jdbc.driver.DatabaseError.throwBatchUpdateException(DatabaseError.java:343)
at oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java:10720)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:242)
... 9 more |
Help! :bug: