These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: Violation de contrainte unique
PostPosted: Wed Jun 07, 2006 7:19 am 
Newbie

Joined: Wed Jun 07, 2006 7:07 am
Posts: 5
Bonjour,

Je suis nouveau sur ce forum et débute en hibernate. Je ne comprends pas pourquoi lors d'une requete de lecture (récupération d'une personne et de ses associations avec les immeubles dans lesquels elle est) j'ai une erreur à cause d'une violation de contrainte (et donc d'une tentative d'insertion dans la base) ; pouvez vous m'expliquer pourquoi lors d'un "select" j'ai cette exception dûe à un "insert" ?
Mon mapping est - il erroné ? Y'a-t-il quelquechose à indiquer pour éviter cet insert ?

Merci d'avance pour votre aide.

(à savoir que les traces que j'ai mises indiquent que les immeubles sont bien récupérés, mais c'est après la récupération, lors du "commit" que l'exception apparait)

Hibernate version: 3

Mapping documents:
Mapping de ma personne pour l'association vers les building :

<set name="listPersonBuilding" table="T_PRSBUILD_EA4">
<key column="PBU_FK_ID_PER" not-null="true"/>
<composite-element class="com.sgcib.pops.business.model.person.PersonBuilding">
<property name="officeName" column="PBU_VA_OFFICENAME" type="java.lang.String" length="50" />
<property name="floor" column="PBU_VA_FLOOR" type="java.lang.String" length="20" />
<property name="picture" column="PBU_VA_OFFICEPICTURE" type="java.lang.String" length="50" />
<property name="active" type="java.lang.Boolean" column="PBU_BL_ACTIVE" />
<property name="phone" column="PBU_VA_PHONE" type="java.lang.String" length="20" />
<property name="cell" column="PBU_VA_CELL" type="java.lang.String" length="20" />
<property name="fax" column="PBU_VA_FAX" type="java.lang.String" length="20" />
<property name="principal" type="java.lang.Boolean" column="PBU_BL_PRINCIPAL" />
<many-to-one name="building" column="PBU_FK_ID_BUI" />
</composite-element>
</set>


Code de ma requete de lecture :

Criteria lCriteria;
Object lResult;
try
{
lCriteria = getSession(true).createCriteria(pObjectClass);
if (pObjectClass.getName().equals("Person"))
{
lCriteria.setFetchMode("listPersonActivity", FetchMode.JOIN);
lCriteria.setFetchMode("listBuildings", FetchMode.JOIN);
}
lCriteria.add(Restrictions.idEq(pPk));
if (lCriteria.list().size() <= 0)
{
throw new FinderException("Item with identifier :" + pPk + " not found.");
}
lResult = lCriteria.list().get(0);
} catch (Throwable lThrowable)
{
sLogger.error("Problem during query execution : " + lThrowable.getMessage());
throw new TechnicalException(lThrowable);
}
return lResult;

Full stack trace of any exception that occurs:

13:04:01,582 - DEBUG - org.springframework.orm.hibernate3.SessionFactoryUtils - Closing Hibernate Session
org.springframework.dao.DataIntegrityViolationException: Hibernate operation: Could not execute JDBC batch update; SQL [insert into T_PRSBUILD_EA4 (PBU_FK_ID_PER, PBU_VA_OFFICENAME, PBU_VA_FLOOR, PBU_VA_OFFICEPICTURE, PBU_BL_ACTIVE, PBU_VA_PHONE, PBU_VA_CELL, PBU_VA_FAX, PBU_BL_PRINCIPAL, PBU_FK_ID_BUI) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)]; ORA-00001: unique constraint (EA4MANAG2.PK_T_PRSBUILD_EA4) violated
; nested exception is java.sql.BatchUpdateException: ORA-00001: unique constraint (EA4MANAG2.PK_T_PRSBUILD_EA4) violated

java.sql.BatchUpdateException: ORA-00001: unique constraint (EA4MANAG2.PK_T_PRSBUILD_EA4) violated

at oracle.jdbc.dbaccess.DBError.throwBatchUpdateException(DBError.java:459)
at oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java:4133)
at org.apache.commons.dbcp.DelegatingStatement.executeBatch(DelegatingStatement.java:294)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:57)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:174)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:74)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:69)
at org.hibernate.jdbc.AbstractBatcher.prepareBatchStatement(AbstractBatcher.java:150)
at org.hibernate.persister.collection.AbstractCollectionPersister.deleteRows(AbstractCollectionPersister.java:975)
at org.hibernate.action.CollectionUpdateAction.execute(CollectionUpdateAction.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: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 org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:584)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:496)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:469)
at org.springframework.transaction.interceptor.TransactionAspectSupport.doCommitTransactionAfterReturning(TransactionAspectSupport.java:266)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:170)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:176)
at $Proxy1.findItemByPk(Unknown Source)
at com.sgcib.pops.persistence.person.dao.IntTestPersonHibernateDAOImpl.testFindItemByPk(IntTestPersonHibernateDAOImpl.java:178)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at junit.framework.TestCase.runTest(TestCase.java:154)
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:478)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:344)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

Name and version of the database you are using: Oracle 10 g


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 07, 2006 8:40 am 
Newbie

Joined: Wed Jun 07, 2006 7:07 am
Posts: 5
Quelques traces de plus (showsql = true) :
on voit bien qu'apres le select, au moment du commit il cherche à faire un delete puis deux insert ... mais je ne vois pas pourquoi.

Merci d'avance.

14:36:42,022 - DEBUG - org.springframework.transaction.support.TransactionSynchronizationManager - Retrieved value [org.springframework.orm.hibernate3.SessionHolder@7c3885] for key [org.hibernate.impl.SessionFactoryImpl@162e295] bound to thread [main]
Hibernate: select this_.PER_N_ID as PER1_0_, this_.PER_DT_REHIRE_DATE as PER2_12_0_, this_.PER_VA_EXTCOMPANY as PER3_12_0_, this_.PER_VA_EXTCPYCODE as PER4_12_0_, this_.PER_DT_CREATION as PER5_12_0_, this_.PER_DT_UPDATE as PER6_12_0_, this_.PER_VA_USUAL_FIRST as PER7_12_0_, this_.PER_VA_USUAL_LAST as PER8_12_0_, this_.PER_VA_WINDOWS_LOGIN as PER9_12_0_, this_.PER_BL_SECURID as PER10_12_0_, this_.PER_BL_BLACKBERRY_ID as PER11_12_0_, this_.PER_BL_POWER_ATTORNEY as PER12_12_0_, this_.PER_VA_EMAIL as PER13_12_0_, this_.PER_VA_FIRST_NAME as PER14_12_0_, this_.PER_VA_MIDDLE_NAME as PER15_12_0_, this_.PER_VA_LAST_NAME as PER16_12_0_, this_.PER_VA_MAIDEN_NAME as PER17_12_0_, this_.PER_VA_BIRTHDATE as PER18_12_0_, this_.PER_VA_IGG as PER19_12_0_, this_.PER_DT_HIRE_DATE as PER20_12_0_, this_.PER_DT_SGCIB_START as PER21_12_0_, this_.PER_DT_CON_TERM as PER22_12_0_, this_.PER_DT_LOA_START as PER23_12_0_, this_.PER_DT_LOA_END as PER24_12_0_, this_.PER_DT_WALK_OUT as PER25_12_0_, this_.PER_BL_ADMIN_MANDATE as PER26_12_0_, this_.PER_N_TASK_MAIL_NOTIF as PER27_12_0_, this_.PER_DT_TASK_NOTIF_UPDATE as PER28_12_0_, this_.PER_BL_REQ_ACC_NOTIF as PER29_12_0_, this_.PER_BL_REQ_REJ_NOTIF as PER30_12_0_, this_.PER_VA_PAGER_NBR as PER31_12_0_, this_.PER_N_FTE as PER32_12_0_, this_.PER_DT_CHANGE_MGR as PER33_12_0_, this_.PER_VA_CHANGE_MGR as PER34_12_0_, this_.PER_DT_CHANGE_POS as PER35_12_0_, this_.PER_VA_CHANGE_POSMGR as PER36_12_0_, this_.PER_DT_CHANGE_DEPT as PER37_12_0_, this_.PER_VA_CHANGE_DEPTMGR as PER38_12_0_, this_.PER_VA_RC_LOCAL_ID as PER39_12_0_, this_.PER_VA_RC_LOCAL_NAME as PER40_12_0_, this_.PER_VA_RESP_TYPE as PER41_12_0_, this_.PER_VA_GLOBAL_HR_ID as PER42_12_0_, this_.PER_VA_LOCAL_HR_ID as PER43_12_0_, this_.PER_VA_VOCALRECORD as PER44_12_0_, this_.PER_VA_PHOTO as PER45_12_0_, this_.PER_BL_PHOTO_VISIBLE as PER46_12_0_, this_.PER_VA_POS_DESC as PER47_12_0_, this_.PER_VA_POS_NAME as PER48_12_0_, this_.PER_VA_HOME_PHONE as PER49_12_0_, this_.PER_VA_HOME_MOBILE as PER50_12_0_, this_.PER_BL_BCPROLE as PER51_12_0_, this_.PER_FK_ID_DPT as PER52_12_0_, this_.PER_FK_ID_LOCAL_DPT as PER53_12_0_, this_.PER_FK_ID_BCO1 as PER54_12_0_, this_.PER_FK_ID_BCO2 as PER55_12_0_, this_.PER_FK_ID_BRO1 as PER56_12_0_, this_.PER_FK_ID_BRO2 as PER57_12_0_, this_.PER_FK_ID_SRC_UPDATE as PER58_12_0_, this_.PER_FK_ID_SRC_CREATE as PER59_12_0_, this_.PER_FK_ID_CIT as PER60_12_0_, this_.PER_FK_ID_SIT as PER61_12_0_, this_.PER_FK_ID_OPER_MGR as PER62_12_0_, this_.PER_FK_ID_RESPONSIBLE as PER63_12_0_, this_.PER_FK_ID_JOB as PER64_12_0_, this_.PER_FK_ID_PTI as PER65_12_0_, this_.PER_FK_ID_CLA as PER66_12_0_, this_.PER_FK_ID_DTJ as PER67_12_0_, this_.PER_FK_ID_HRS as PER68_12_0_, this_.PER_FK_ID_CPY as PER69_12_0_, this_.PER_FK_ID_COU as PER70_12_0_, listperson2_.PAC_FK_ID_PER as PAC1_2_, listperson2_.PAC_DT_START as PAC2_2_, listperson2_.PAC_FK_ID_SCO as PAC3_2_, listperson2_.PAC_FK_ID_ACT as PAC4_2_, listperson2_.PAC_FK_ID_PER_MGR as PAC5_2_ from T_PERSON_EA4 this_ left outer join T_PERSACTIV_EA4 listperson2_ on this_.PER_N_ID=listperson2_.PAC_FK_ID_PER where this_.PER_N_ID = ?
Hibernate: select listperson0_.PBU_FK_ID_PER as PBU1_0_, listperson0_.PBU_VA_OFFICENAME as PBU2_0_, listperson0_.PBU_VA_FLOOR as PBU3_0_, listperson0_.PBU_VA_OFFICEPICTURE as PBU4_0_, listperson0_.PBU_BL_ACTIVE as PBU5_0_, listperson0_.PBU_VA_PHONE as PBU6_0_, listperson0_.PBU_VA_CELL as PBU7_0_, listperson0_.PBU_VA_FAX as PBU8_0_, listperson0_.PBU_BL_PRINCIPAL as PBU9_0_, listperson0_.PBU_FK_ID_BUI as PBU10_0_ from T_PRSBUILD_EA4 listperson0_ where listperson0_.PBU_FK_ID_PER=?
14:36:42,225 - DEBUG - org.springframework.transaction.interceptor.TransactionInterceptor - Invoking commit for transaction on com.sgcib.pops.persistence.common.ICommonDAO.findItemByPk
14:36:42,225 - DEBUG - org.springframework.orm.hibernate3.HibernateTransactionManager - Triggering beforeCommit synchronization
14:36:42,225 - DEBUG - org.springframework.orm.hibernate3.HibernateTransactionManager - Triggering beforeCompletion synchronization
14:36:42,225 - DEBUG - org.springframework.orm.hibernate3.HibernateTransactionManager - Initiating transaction commit
14:36:42,225 - DEBUG - org.springframework.orm.hibernate3.HibernateTransactionManager - Committing Hibernate transaction on Session [org.hibernate.impl.SessionImpl@13576a2]
Hibernate: delete from T_PRSBUILD_EA4 where PBU_FK_ID_PER=? and PBU_VA_OFFICENAME=? and PBU_VA_FLOOR=? and PBU_VA_OFFICEPICTURE=? and PBU_BL_ACTIVE=? and PBU_VA_PHONE=? and PBU_VA_CELL=? and PBU_VA_FAX=? and PBU_BL_PRINCIPAL=? and PBU_FK_ID_BUI=?
Hibernate: insert into T_PRSBUILD_EA4 (PBU_FK_ID_PER, PBU_VA_OFFICENAME, PBU_VA_FLOOR, PBU_VA_OFFICEPICTURE, PBU_BL_ACTIVE, PBU_VA_PHONE, PBU_VA_CELL, PBU_VA_FAX, PBU_BL_PRINCIPAL, PBU_FK_ID_BUI) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into T_PRSBUILD_EA4 (PBU_FK_ID_PER, PBU_VA_OFFICENAME, PBU_VA_FLOOR, PBU_VA_OFFICEPICTURE, PBU_BL_ACTIVE, PBU_VA_PHONE, PBU_VA_CELL, PBU_VA_FAX, PBU_BL_PRINCIPAL, PBU_FK_ID_BUI) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
14:36:42,288 - DEBUG - org.springframework.jdbc.support.SQLErrorCodesFactory - Looking up default SQLErrorCodes for DataSource [org.apache.commons.dbcp.BasicDataSource@36527f]
14:36:42,288 - DEBUG - org.springframework.jdbc.support.SQLErrorCodesFactory - Database product name found in cache for DataSource [org.apache.commons.dbcp.BasicDataSource@36527f]: name is 'Oracle'
14:36:42,288 - DEBUG - org.springframework.jdbc.support.SQLErrorCodesFactory - SQL error codes for 'Oracle' found
14:36:42,288 - DEBUG - org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator - Translating SQLException with SQLState '23000' and errorCode '1' and message [ORA-00001: unique constraint (EA4MANAG2.PK_T_PRSBUILD_EA4) violated
]; SQL was [insert into T_PRSBUILD_EA4 (PBU_FK_ID_PER, PBU_VA_OFFICENAME, PBU_VA_FLOOR, PBU_VA_OFFICEPICTURE, PBU_BL_ACTIVE, PBU_VA_PHONE, PBU_VA_CELL, PBU_VA_FAX, PBU_BL_PRINCIPAL, PBU_FK_ID_BUI) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)] for task [Hibernate operation: Could not execute JDBC batch update]
14:36:42,288 - DEBUG - org.springframework.orm.hibernate3.HibernateTransactionManager - Initiating transaction rollback on commit exception
org.springframework.dao.DataIntegrityViolationException: Hibernate operation: Could not execute JDBC batch update; SQL [insert into T_PRSBUILD_EA4 (PBU_FK_ID_PER, PBU_VA_OFFICENAME, PBU_VA_FLOOR, PBU_VA_OFFICEPICTURE, PBU_BL_ACTIVE, PBU_VA_PHONE, PBU_VA_CELL, PBU_VA_FAX, PBU_BL_PRINCIPAL, PBU_FK_ID_BUI) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)]; ORA-00001: unique constraint (EA4MANAG2.PK_T_PRSBUILD_EA4) violated
; nested exception is java.sql.BatchUpdateException: ORA-00001: unique constraint (EA4MANAG2.PK_T_PRSBUILD_EA4) violated


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 12, 2006 6:16 pm 
Proxool Developer
Proxool Developer

Joined: Tue Aug 26, 2003 10:42 am
Posts: 373
Location: Belgium
A la cloture de la session, Hibernate determine quels sont les changements qui ont ete operes sur les objets presents dans la session.

Pour ce faire, Hibernate compare leur etat par rapport a une version initiale (snapshot) qu'il a 'mis de cote' lors du premier chargement de l'objet (toujours dans le cadre de la meme session).

Le processus est assez simple, pour chaque objet dans la session au moment de la cloture, Hibernate recherche la version snapshot initiale. Comment faire pour savoir qu'il s'agit de la meme instance? Pour ce faire, il utilise la classe plus l'identifiant de l'objet (pour plus d'information, tu trouvers un chapitre a ce sujet dans la doc - relatif au probleme d'identitie, java vs db).

TON PROBLEME vient probablement du fait que ton set est compose d'element composites et non d'entites au sens de Hibernate. Ces elements n'ont donc pas d'identifiant unique. Dans ce cas, par defaut, Hibernate supprime le contenu du set et le reinsere... D'ou le delete suivi d'autant d'insert qu'il y a d'elements dans ton Set.

La solution consiste a reimplementer correctement les methodes equals (et donc hashCode()) des elements de ton Set de maniere a ce que deux instances differentes mais representant les memes concepts/donnees soient egaux. Hibernate (et donc le Set egalement) pourra se baser sur la methode equals() pour determiner si il y a eu des changements ou pas...

J'espere avoir ete clair...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 13, 2006 4:19 am 
Newbie

Joined: Wed Jun 07, 2006 7:07 am
Posts: 5
Merci beaucoup pour cette explication tr-ès claire, et effectivement cela a solutionné le problème.
J'avais bien noté qu'il fallait surcharger les méthodes equals() et haschode() mais je ne le faisais que pour les entités (Person et Building) ; j'avais omis les éléments composites.

Encore merci pour cette réponse claire et salvatrice :)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 13, 2006 4:37 am 
Proxool Developer
Proxool Developer

Joined: Tue Aug 26, 2003 10:42 am
Posts: 373
Location: Belgium
Heureux que mon explication t'ai aide...
Un petit credit sur mon compte serait alors bien venu....


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 13, 2006 6:45 am 
Newbie

Joined: Wed Jun 07, 2006 7:07 am
Posts: 5
c'est fait ; Désolé je suis nouveau sur ce forum et je n'ai pas encore tous les réflexes :p


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.