2 votes

Impossible de supprimer ou de mettre à jour une ligne parent : une contrainte de clé étrangère échoue et la hiérarchie de l'entité ne doit pas changer.

C'est une question difficile. Je sais qu'il y a beaucoup de questions de ce genre ici, mais mon problème est que je n'ai pas le droit de changer/éditer la hiérarchie des classes, parce qu'il s'agit d'une application déjà productive, qui si elle est changée fondamentalement, doit être testée minutieusement et cela coûterait trop cher. Même s'il y a quelque chose de stupide dans la hiérarchie, je n'ai pas le droit de la modifier, car l'application entière pourrait cesser de fonctionner, et il faudra de longues heures pour la remettre en marche. (Méchant, je sais). Donc, fondamentalement, ce que je cherche ici est une sorte de solution de contournement, qui me donnera une chance de supprimer des instances d'entités sans violation de contrainte et sans changer la hiérarchie.

Au sommet de la hiérarchie se trouve une entité "Request", qui se divise ensuite en "Import Request" et "Export Request". Ainsi, ImportRequest étend Request et ExportRequest étend Request. Chaque ExportRequest a un ensemble de positions, qui est défini comme suit :

@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "antrag_id")
@ForeignKey(name= "FK_EXPAN_POS")
public Set<Position> getPosition() {
    try {
        position.size();
    } catch (Exception e) {
        // NO Need just make hibernate load the list
    }

    return position;
}

La position est juste une autre entité. Et chaque ImportRequest a un Set d'ImportPosition, comme ça :

@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "antrag_id")
@ForeignKey(name= "FK_IMPAN_POS")
public Set<ImportPosition> getImportPosition() {

    try {
        importPosition.size();
    }
    catch (Exception e)
    {
        // hallo 
    }
    return importPosition;
}

alors que ImportPosition s'étend à Position.

Chaque position n'a qu'une seule sonde qui lui est assignée, qui est définie comme ça :

@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "probe")
@ForeignKey(name = "FK_POS_PROB")
public Probe getProbe() {
    return probe;
}

Implicitement, chaque ImportPosition en aura un aussi, car il étend la Position. Ce qui fonctionne également au niveau de la base de données.

Je supprime un IMportRequets avec une méthode comme celle-ci :

@Transactional
public void cleanArchivedRequests() {
    List<Integer> archivedRequestList = requestDao.retrieveAllArchivedRequests();
    for (Integer antragId : archivedRequestList) {
        Request requestToDelete = requestDao.loadById(requestId);
        requestDao.delete(requestToDelete);

        StringBuffer protocolEntry = new StringBuffer("Request mit der ID: ");
        protocolEntry.append(requestId);
        protocolEntry.append(" gelöscht!");
        protocol(protocolEntry.toString());
    }
}

Apparemment, Hibernate a des problèmes pour supprimer la sonde qui est référencée par un ImportPosition dans un ImportRequet, car j'obtiens ceci :

 [java] java.sql.BatchUpdateException: Cannot delete or update a parent row: a foreign key constraint fails (`position`, CONSTRAINT `FK_POS_PROB` FOREIGN KEY (`probe`) REFERENCES `probe` (`id`))

requestDao est injecté via Spring avec applicationContext-daos.xml dans ma classe, où se trouve la méthode "cleanArchivedRequests".

Ma question est donc la suivante : compte tenu de cette hiérarchie, est-il possible de supprimer avec succès les instances de sonde de l'ImportPosition et, si oui, comment cela fonctionne-t-il ? J'ai déjà essayé de supprimer d'abord les instances Probe, puis l'ImportRequest, mais cela n'a pas fonctionné, soi-disant parce que tout se passe en une seule transaction. C'est du moins ce que j'explique.

PS. Le Stacktrace entier va comme ça :

 [java] java.sql.BatchUpdateException: Cannot delete or update a parent row: a foreign key constraint fails (`position`, CONSTRAINT `FK_POS_PROB` FOREIGN KEY (`probe`) REFERENCES `probe` (`id`))
 [java]     at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1257)
 [java]     at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:943)
 [java]     at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeBatch(NewProxyPreparedStatement.java:1723)
 [java]     at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
 [java]     at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
 [java]     at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:114)
 [java]     at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:109)
 [java]     at org.hibernate.jdbc.AbstractBatcher.prepareBatchStatement(AbstractBatcher.java:244)
 [java]     at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2507)
 [java]     at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2725)
 [java]     at org.hibernate.action.EntityDeleteAction.execute(EntityDeleteAction.java:97)
 [java]     at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
 [java]     at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:263)
 [java]     at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:172)
 [java]     at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
 [java]     at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
 [java]     at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1027)
 [java]     at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:365)
 [java]     at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)
 [java]     at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:655)
 [java]     at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:732)
 [java]     at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:701)
 [java]     at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:321)
 [java]     at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:116)
 [java]     at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
 [java]     at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
 [java]     at $Proxy33.cleanArchivedRequests(Unknown Source)
 [java]     at de.fast.hama.util.HamaRequestArchiver.main(HamaRequestArchiver.java:66)
 [java] 2012-06-22 17:56:07,925 [DEBUG] - com.mchange.v2.sql.SqlUtils.toSQLException:85 - Attempted to convert SQLException to SQLException. Leaving it alone. [SQLState: 23000; errorCode: 1451]
 [java] java.sql.BatchUpdateException: Cannot delete or update a parent row: a foreign key constraint fails (`position`, CONSTRAINT `FK_POS_PROB` FOREIGN KEY (`probe`) REFERENCES `probe` (`id`))
 [java]     at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1257)
 [java]     at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:943)
 [java]     at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeBatch(NewProxyPreparedStatement.java:1723)
 [java]     at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
 [java]     at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
 [java]     at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:114)
 [java]     at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:109)
 [java]     at org.hibernate.jdbc.AbstractBatcher.prepareBatchStatement(AbstractBatcher.java:244)
 [java]     at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2507)
 [java]     at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2725)
 [java]     at org.hibernate.action.EntityDeleteAction.execute(EntityDeleteAction.java:97)
 [java]     at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
 [java]     at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:263)
 [java]     at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:172)
 [java]     at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
 [java]     at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
 [java]     at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1027)
 [java]     at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:365)
 [java]     at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)
 [java]     at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:655)
 [java]     at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:732)
 [java]     at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:701)
 [java]     at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:321)
 [java]     at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:116)
 [java]     at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
 [java]     at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
 [java]     at $Proxy33.cleanArchivedRequests(Unknown Source)
 [java]     at de.fast.hama.util.HamaRequestArchiver.main(HamaRequestArchiver.java:66)
 [java] 2012-06-22 17:56:07,925 [DEBUG] - com.mchange.v2.c3p0.impl.DefaultConnectionTester.statusOnException:126 - Testing a Connection in response to an Exception:
 [java] java.sql.BatchUpdateException: Cannot delete or update a parent row: a foreign key constraint fails (`position`, CONSTRAINT `FK_POS_PROB` FOREIGN KEY (`probe`) REFERENCES `probe` (`id`))
 [java]     at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1257)
 [java]     at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:943)
 [java]     at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeBatch(NewProxyPreparedStatement.java:1723)
 [java]     at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
 [java]     at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
 [java]     at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:114)
 [java]     at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:109)
 [java]     at org.hibernate.jdbc.AbstractBatcher.prepareBatchStatement(AbstractBatcher.java:244)
 [java]     at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2507)
 [java]     at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2725)
 [java]     at org.hibernate.action.EntityDeleteAction.execute(EntityDeleteAction.java:97)
 [java]     at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
 [java]     at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:263)
 [java]     at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:172)
 [java]     at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
 [java]     at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
 [java]     at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1027)
 [java]     at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:365)
 [java]     at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)
 [java]     at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:655)
 [java]     at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:732)
 [java]     at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:701)
 [java]     at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:321)
 [java]     at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:116)
 [java]     at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
 [java]     at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
 [java]     at $Proxy33.cleanArchivedRequests(Unknown Source)
 [java]     at de.fast.hama.util.HamaRequestArchiver.main(HamaRequestArchiver.java:66)
 [java] 22.06.2012 17:56:07 org.hibernate.util.JDBCExceptionReporter logExceptions
 [java] WARNUNG: SQL Error: 1451, SQLState: 23000
 [java] 22.06.2012 17:56:07 org.hibernate.util.JDBCExceptionReporter logExceptions
 [java] SCHWERWIEGEND: Cannot delete or update a parent row: a foreign key constraint fails (`position`, CONSTRAINT `FK_POS_PROB` FOREIGN KEY (`probe`) REFERENCES `probe` (`id`))
 [java] 22.06.2012 17:56:07 org.hibernate.event.def.AbstractFlushingEventListener performExecutions
 [java] SCHWERWIEGEND: Could not synchronize database state with session
 [java] org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
 [java]     at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:94)
 [java]     at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
 [java]     at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
 [java]     at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:114)
 [java]     at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:109)
 [java]     at org.hibernate.jdbc.AbstractBatcher.prepareBatchStatement(AbstractBatcher.java:244)
 [java]     at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2507)
 [java]     at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2725)
 [java]     at org.hibernate.action.EntityDeleteAction.execute(EntityDeleteAction.java:97)
 [java]     at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
 [java]     at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:263)
 [java]     at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:172)
 [java]     at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
 [java]     at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
 [java]     at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1027)
 [java]     at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:365)
 [java]     at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)
 [java]     at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:655)
 [java]     at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:732)
 [java]     at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:701)
 [java]     at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:321)
 [java]     at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:116)
 [java]     at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
 [java]     at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
 [java]     at $Proxy33.cleanArchivedRequests(Unknown Source)
 [java]     at de.fast.hama.util.HamaRequestArchiver.main(HamaRequestArchiver.java:66)
 [java] Caused by: java.sql.BatchUpdateException: Cannot delete or update a parent row: a foreign key constraint fails (`position`, CONSTRAINT `FK_POS_PROB` FOREIGN KEY (`probe`) REFERENCES `probe` (`id`))
 [java]     at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1257)
 [java]     at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:943)
 [java]     at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeBatch(NewProxyPreparedStatement.java:1723)
 [java]     at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
 [java]     at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
 [java]     ... 23 more
 [java] 2012-06-22 17:56:07,941 [DEBUG] - org.springframework.orm.hibernate3.HibernateTransactionManager.doRollbackOnCommitException:871 - Initiating transaction rollback after commit exception
 [java] org.springframework.dao.DataIntegrityViolationException: Could not execute JDBC batch update; nested exception is org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
 [java]     at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:636)
 [java]     at org.springframework.orm.hibernate3.HibernateTransactionManager.convertHibernateAccessException(HibernateTransactionManager.java:789)
 [java]     at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:663)
 [java]     at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:732)
 [java]     at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:701)
 [java]     at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:321)
 [java]     at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:116)
 [java]     at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
 [java]     at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
 [java]     at $Proxy33.cleanArchivedRequests(Unknown Source)
 [java]     at de.fast.hama.util.HamaRequestArchiver.main(HamaRequestArchiver.java:66)
 [java] Caused by: org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
 [java]     at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:94)
 [java]     at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
 [java]     at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
 [java]     at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:114)
 [java]     at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:109)
 [java]     at org.hibernate.jdbc.AbstractBatcher.prepareBatchStatement(AbstractBatcher.java:244)
 [java]     at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2507)
 [java]     at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2725)
 [java]     at org.hibernate.action.EntityDeleteAction.execute(EntityDeleteAction.java:97)
 [java]     at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
 [java]     at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:263)
 [java]     at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:172)
 [java]     at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
 [java]     at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
 [java]     at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1027)
 [java]     at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:365)
 [java]     at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)
 [java]     at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:655)
 [java]     ... 8 more
 [java] Caused by: java.sql.BatchUpdateException: Cannot delete or update a parent row: a foreign key constraint fails (`position`, CONSTRAINT `FK_POS_PROB` FOREIGN KEY (`probe`) REFERENCES `probe` (`id`))
 [java]     at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1257)
 [java]     at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:943)
 [java]     at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeBatch(NewProxyPreparedStatement.java:1723)
 [java]     at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
 [java]     at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
 [java]     ... 23 more
 [java] 2012-06-22 17:56:07,941 [DEBUG] - org.springframework.orm.hibernate3.HibernateTransactionManager.doRollback:670 - Rolling back Hibernate transaction on Session [org.hibernate.impl.SessionImpl@1c10834]
 [java] 2012-06-22 17:56:07,956 [DEBUG] - org.springframework.orm.hibernate3.HibernateTransactionManager.doCleanupAfterCompletion:730 - Closing Hibernate Session [org.hibernate.impl.SessionImpl@1c10834] after transaction
 [java] 2012-06-22 17:56:07,956 [DEBUG] - org.springframework.orm.hibernate3.SessionFactoryUtils.closeSession:789 - Closing Hibernate Session
 [java] 2012-06-22 17:56:07,956 [DEBUG] - com.mchange.v2.resourcepool.BasicResourcePool.trace:1644 - trace com.mchange.v2.resourcepool.BasicResourcePool@1a37fac [managed: 10, unused: 9, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection@f1b25e)
 [java] Exception in thread "main" org.springframework.dao.DataIntegrityViolationException: Could not execute JDBC batch update; nested exception is org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
 [java]     at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:636)
 [java]     at org.springframework.orm.hibernate3.HibernateTransactionManager.convertHibernateAccessException(HibernateTransactionManager.java:789)
 [java]     at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:663)
 [java]     at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:732)
 [java]     at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:701)
 [java]     at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:321)
 [java]     at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:116)
 [java]     at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
 [java]     at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
 [java]     at $Proxy33.cleanArchivedRequests(Unknown Source)
 [java]     at de.fast.hama.util.HamaRequestArchiver.main(HamaRequestArchiver.java:66)
 [java] Caused by: org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
 [java]     at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:94)
 [java]     at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
 [java]     at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
 [java]     at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:114)
 [java]     at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:109)
 [java]     at org.hibernate.jdbc.AbstractBatcher.prepareBatchStatement(AbstractBatcher.java:244)
 [java]     at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2507)
 [java]     at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2725)
 [java]     at org.hibernate.action.EntityDeleteAction.execute(EntityDeleteAction.java:97)
 [java]     at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
 [java]     at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:263)
 [java]     at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:172)
 [java]     at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
 [java]     at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
 [java]     at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1027)
 [java]     at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:365)
 [java]     at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)
 [java]     at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:655)
 [java]     ... 8 more
 [java] Caused by: java.sql.BatchUpdateException: Cannot delete or update a parent row: a foreign key constraint fails (`position`, CONSTRAINT `FK_POS_PROB` FOREIGN KEY (`probe`) REFERENCES `probe` (`id`))
 [java]     at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1257)
 [java]     at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:943)
 [java]     at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeBatch(NewProxyPreparedStatement.java:1723)
 [java]     at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
 [java]     at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
 [java]     ... 23 more
 [java] Java Result: 1

0voto

Peter Bratton Points 3343

Il ne semble pas que l'héritage Hibernate soit le problème ici... vous essayez de supprimer des lignes de la table Probe qui sont référencées via des clés étrangères dans la table Position (et sa hiérarchie de classe).

Je présume que puisque vous utilisez le @ForeignKey annotation que vous utilisez un script généré à partir de vos objets Hibernate pour générer votre base de données DDL script. Ces contraintes par défaut ne spécifient aucun comportement sur la suppression des références, d'où l'erreur que vous voyez. Vous pouvez soit modifier les contraintes pour spécifier un comportement @OnDelete, soit définir explicitement la référence de clé étrangère à null pour chaque objet Probe que vous supprimez.

Prograide.com

Prograide est une communauté de développeurs qui cherche à élargir la connaissance de la programmation au-delà de l'anglais.
Pour cela nous avons les plus grands doutes résolus en français et vous pouvez aussi poser vos propres questions ou résoudre celles des autres.

Powered by:

X