Impossible de sauvegarder
bonsoir,
j'ai un petit probleme de sauvegarde,
j'ai créer deux eleves, chaque éleve "étudie" dans une école relation manyToOne
voici le code :
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 32 33 34
|
SessionFactory factory = config.buildSessionFactory();
Session session = factory.openSession();
session.beginTransaction();
Ecole ecole =new Ecole();
ecole.setNameEcole("Nathan");
Eleves eleve =new Eleves();
eleve.setNom("Dzar");
eleve.setPrenom("Dominique");
eleve.setAge(36);
eleve.setEcole(ecole);
Eleves eleve2 =new Eleves();
eleve2.setAge(27);
eleve2.setNom("Pascale");
eleve2.setPrenom("Dauphin");
eleve2.setEcole(ecole);
session.save(ecole);
session.save(eleve);
session.save(eleve2);
session.beginTransaction().commit(); |
le probleme c'est que ce code génere une erreur de clé :
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| Hibernate: select max(ID) from TEST.ECOLE
Hibernate: select max(ID) from TEST.ELEVES
Hibernate: insert into TEST.ECOLE (NAMEECOLE, ID) values (?, ?)
Hibernate: insert into TEST.ELEVES (NOM, PRENOM, AGE, ECOLE, ID) values (?, ?, ?, ?, ?)
Hibernate: insert into TEST.ELEVES (NOM, PRENOM, AGE, ECOLE, ID) values (?, ?, ?, ?, ?)
Exception in thread "main" org.hibernate.exception.ConstraintViolationException: could not insert: [com.hibernate.test.Eleves]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:96)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2436)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2856)
at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:79)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:273)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:265)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:184)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:383)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:133)
at com.hibernate.test.Test.main(Test.java:97)
Caused by: java.sql.SQLIntegrityConstraintViolationException: INSERT sur la table 'ELEVES' a entraîné la violation de la contrainte de clé externe 'FK7A0E06465914D2D2' pour la clé (3). L'instruction a été annulée.
at org.apache.derby.impl.jdbc.SQLExceptionFactory40.getSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.TransactionResourceImpl.wrapInSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.TransactionResourceImpl.handleException(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedConnection.handleException(Unknown Source)
at org.apache.derby.impl.jdbc.ConnectionChild.handleException(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedStatement.executeStatement(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedPreparedStatement.executeStatement(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedPreparedStatement.executeUpdate(Unknown Source)
at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:46)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2416)
... 11 more
Caused by: java.sql.SQLException: INSERT sur la table 'ELEVES' a entraîné la violation de la contrainte de clé externe 'FK7A0E06465914D2D2' pour la clé (3). L'instruction a été annulée.
at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.SQLExceptionFactory40.wrapArgsForTransportAcrossDRDA(Unknown Source)
... 22 more
Caused by: ERROR 23503: INSERT sur la table 'ELEVES' a entraîné la violation de la contrainte de clé externe 'FK7A0E06465914D2D2' pour la clé (3). L'instruction a été annulée.
at org.apache.derby.iapi.error.StandardException.newException(Unknown Source)
at org.apache.derby.impl.sql.execute.ForeignKeyRIChecker.doCheck(Unknown Source)
at org.apache.derby.impl.sql.execute.GenericRIChecker.doCheck(Unknown Source)
at org.apache.derby.impl.sql.execute.RISetChecker.doFKCheck(Unknown Source)
at org.apache.derby.impl.sql.execute.InsertResultSet.normalInsertCore(Unknown Source)
at org.apache.derby.impl.sql.execute.InsertResultSet.open(Unknown Source)
at org.apache.derby.impl.sql.GenericPreparedStatement.executeStmt(Unknown Source)
at org.apache.derby.impl.sql.GenericPreparedStatement.execute(Unknown Source)
... 16 more |
lorsque je sauvegarde avec un seul eleve cela fonctionne bien ! , je pense que l'objet ecole ne peut pas être sauvegardé plus de 2 fois à cause de la clé unique or je ne sais pas comment résoudre ce probleme .
merci !