Hi all!
Im using Spring 3.0.5 for dependency injection, and Transaction Support.
In my abstract DAO Class, I use this code:Code:<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> <property name="showSql" value="false" /> <property name="generateDdl" value="false" /> <!-- Dialeto Oracle --> <property name="databasePlatform" value="org.hibernate.dialect.PostgreSQLDialect" /> </bean> </property> <property name="jpaProperties"> <props> <prop key="hibernate.format_sql">true</prop> <prop key="cache.provider_class">org.hibernate.cache.NoCacheProvider</prop> </props> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory" /> </bean> <tx:annotation-driven />
This works fine. BUT, if try for example, delete an entity that haves a Foreign Key Constraint, course, I got a Exception Throwed:Code:@Transactional(propagation=Propagation.REQUIRED) public void deleteById(T entityId) throws DataAccessException { if(entityId != null) { E entity = findById(entityId); if(entity != null) { getEntityManager().remove(entity); } } } @Transactional(propagation=Propagation.REQUIRED) @SuppressWarnings("unchecked") public void delete(E entity) { if(getEntityManager().contains(entity)) { getEntityManager().remove(entity); } else { deleteById( ((DefaultEntity<T>)entity).getId() ); } }
The problem, is after that, ALWAYS that I try to persist any entities of the same type, after the Update, or INSERT statement, the system try again, to delete the entity that haves throwed the exception by the constraint.Code:Caused by: javax.persistence.RollbackException: Error while committing the transaction at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:93) at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerSynchronization.afterCommit(ExtendedEntityManagerCreator.java:478) ... 39 more Caused by: javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not delete: [br.com.simus.supera.ciclonev2.persistence.atividade.AtividadeConferenciaPrecoModelo#56] at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1214) at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1147) at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:81) ... 40 more Caused by: org.hibernate.exception.ConstraintViolationException: could not delete: [br.com.simus.supera.ciclonev2.persistence.atividade.AtividadeConferenciaPrecoModelo#56] at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:96) at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66) at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2728) at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2911) at org.hibernate.action.EntityDeleteAction.execute(EntityDeleteAction.java:97) 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:189) 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 org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:76) ... 40 more
SQL Statements logged:
The initial Delete, that get constraint error:
After that, I try to insert a new Entity of the same type. This is the generated statement:Code:LOG: execute S_1: BEGIN LOG: execute <unnamed>: delete from t030_1_1_executores where t030_1_1_modelo_e=$1 DETAIL: parameters: $1 = '56' LOG: execute <unnamed>: delete from t030_1_2_motivos where t030_1_2_modelo_e=$1 DETAIL: parameters: $1 = '56' LOG: execute <unnamed>: delete from t030_1_conferencia_preco_modelo where t030_1_conferencia_preco_modelo_iu=$1 DETAIL: parameters: $1 = '56' ERROR: update or delete on table "t030_1_conferencia_preco_modelo" violates foreign key constraint "t012_job_modelo_fk" on table "t012_job_cria_acao_conferencia_preco" DETAIL: Key (t030_1_conferencia_preco_modelo_iu)=(56) is still referenced from table "t012_job_cria_acao_conferencia_preco". STATEMENT: delete from t030_1_conferencia_preco_modelo where t030_1_conferencia_preco_modelo_iu=$1 ERROR: current transaction is aborted, commands ignored until end of transaction block LOG: execute S_2: ROLLBACK
As you can see, Hibernate try to delete again the entity, I dont invoke any delete methods. I only try to insert a new entity.Code:LOG: execute <unnamed>: select nextval ('t030_atividade_modelo_base_t030_atividade_modelo_base_iu_seq') LOG: execute <unnamed>: select motivo0_.t020_motivo_nao_conferencia_iu as t1_9_0_, motivo0_.t020_descricao as t2_9_0_, motivo0_.t020_motivo as t3_9_0_ from t020_motivos_nao_conferencia motivo0_ where motivo0_.t020_motivo_nao_conferencia_iu=$1 DETAIL: parameters: $1 = '33' LOG: execute <unnamed>: insert into t030_atividade_modelo_base (t030_data_criacao, t030_descricao, t030_nome, t030_usuario_criacao_e, t030_atividade_modelo_base_iu) values ($1, $2, $3, $4, $5) DETAIL: parameters: $1 = '2011-04-07 11:23:26.443', $2 = NULL, $3 = 'teste160', $4 = '26', $5 = '71' LOG: execute <unnamed>: insert into t030_1_conferencia_preco_modelo (t030_1_duracao_minutos, t030_1_conferencia_preco_modelo_iu) values ($1, $2) DETAIL: parameters: $1 = '10', $2 = '71' LOG: execute <unnamed>: insert into t030_1_1_executores (t030_1_1_modelo_e, t030_1_1_usuario_e) values ($1, $2) DETAIL: parameters: $1 = '71', $2 = '24' LOG: execute <unnamed>: insert into t030_1_2_motivos (t030_1_2_modelo_e, t030_1_2_motivo_e) values ($1, $2) DETAIL: parameters: $1 = '71', $2 = '33' LOG: execute <unnamed>: delete from t030_1_conferencia_preco_modelo where t030_1_conferencia_preco_modelo_iu=$1 DETAIL: parameters: $1 = '56' ERROR: update or delete on table "t030_1_conferencia_preco_modelo" violates foreign key constraint "t030_2_motivos_modelo_fk" on table "t030_1_2_motivos" DETAIL: Key (t030_1_conferencia_preco_modelo_iu)=(56) is still referenced from table "t030_1_2_motivos". STATEMENT: delete from t030_1_conferencia_preco_modelo where t030_1_conferencia_preco_modelo_iu=$1
Someone could help? I cant found any post related this problem!
Many thanks, and sorry by bad english.


Reply With Quote