Spring Data / JPA / Hibernate:不提交更改

时间:2015-06-01 11:11:57

标签: spring hibernate spring-data

我正在尝试执行一个必须从子选择中删除项目的查询。

环境:

  • Wildfly 8.2
  • Spring Framework 4.1.6
  • Spring Data 1.8.0
  • Hibernate 4.3.10
  • MySQL 5

这是存储库:

public interface MySecondEntityRepository extends JpaRepository<MySecondEntity, UUID> {
    @Modifying
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    @Query("DELETE FROM MySecondEntity e WHERE e.uuid IN ("
             + "SELECT e2.uuid "
             + "FROM MySecondEntity e2, MyEntity e1 "
             + "WHERE e2.entityUUID = e1.uuid "
             + "AND e1.uuid = :uuid "
             + "AND e1.*** = ..."
             + "AND e1.*** = ..."
         + ")")
    public void deleteByEntityUUID(@Param("uuid") UUID entityUUID, ...);
}

以下是事务管理器的XML定义:

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

其中entityManagerFactoryLocalContainerEntityManagerFactoryBean

我正在使用它们而不是依赖提供者。

在日志中,我可以看到:

SQL | insert into HT_my_entity select ...
SQL | delete from my_entity where uuid IN (select uuid FROM HT_my_entity);

当我手动执行select ...时,我会删除正确的uuids。但是当spring / hibernate执行它们时,即使使用Propagation.REQUIRES_NEW事务也不会提交。没有例外。

完整日志:

14:41:45,073 | DEBUG | AnnotationTransactionAttri:108  | Adding transactional method 'DefaultJpaRepositoryImpl.deleteByEntityUUID' with attribute: PROPAGATION_REQUIRES_NEW,ISOLATION_DEFAULT; ''
14:41:45,073 | DEBUG | JpaTransactionManager     :367  | Creating new transaction with name [xxx.spring.jpa.factory.DefaultJpaRepositoryImpl.deleteByEntityUUID]: PROPAGATION_REQUIRES_NEW,ISOLATION_DEFAULT; ''
14:41:45,074 | DEBUG | JpaTransactionManager     :371  | Opened new EntityManager [org.hibernate.jpa.internal.EntityManagerImpl@cd414] for JPA transaction
14:41:45,075 | DEBUG | AbstractTransactionImpl   :160  | begin
14:41:45,075 | DEBUG | LogicalConnectionImpl     :226  | Obtaining JDBC connection
14:41:45,075 | DEBUG | LogicalConnectionImpl     :232  | Obtained JDBC connection
14:41:45,076 | DEBUG | JdbcTransaction           :69   | initial autocommit status: true
14:41:45,076 | DEBUG | JdbcTransaction           :71   | disabling autocommit
14:41:45,076 | DEBUG | JpaTransactionManager     :403  | Exposing JPA transaction as JDBC transaction [org.springframework.orm.jpa.vendor.HibernateJpaDialect$HibernateConnectionHandle@7d7135]
14:41:45,078 | DEBUG | JpaTransactionManager     :334  | Found thread-bound EntityManager [org.hibernate.jpa.internal.EntityManagerImpl@cd414] for JPA transaction
14:41:45,079 | DEBUG | JpaTransactionManager     :417  | Suspending current transaction, creating new transaction with name [xxx.spring.jpa.factory.DefaultJpaRepositoryImpl.deleteByEntityUUID]
14:41:45,082 | DEBUG | JpaTransactionManager     :371  | Opened new EntityManager [org.hibernate.jpa.internal.EntityManagerImpl@35e880] for JPA transaction
14:41:45,082 | DEBUG | AbstractTransactionImpl   :160  | begin
14:41:45,082 | DEBUG | LogicalConnectionImpl     :226  | Obtaining JDBC connection
14:41:45,083 | DEBUG | LogicalConnectionImpl     :232  | Obtained JDBC connection
14:41:45,083 | DEBUG | JdbcTransaction           :69   | initial autocommit status: true
14:41:45,083 | DEBUG | JdbcTransaction           :71   | disabling autocommit
14:41:45,084 | DEBUG | JpaTransactionManager     :403  | Exposing JPA transaction as JDBC transaction [org.springframework.orm.jpa.vendor.HibernateJpaDialect$HibernateConnectionHandle@1b46a77]
14:41:45,223 | DEBUG | SQL                       :109  | insert into HT_my_entity select my_second_0_.uuid as uuid from my_second_entity my_second_0_ where my_second_0_.uuid in (select my_second_1_.uuid from my_second_entity my_second_1_ cross join my_entity my_entity_2_ where my_second_1_.entity_uuid=my_entity_2_.uuid and my_entity_2_.uuid=?)
14:41:45,232 | DEBUG | SQL                       :109  | delete from my_entity where uuid IN (select uuid FROM HT_my_entity);
14:41:45,333 | DEBUG | JpaTransactionManager     :755  | Initiating transaction commit
14:41:45,334 | DEBUG | JpaTransactionManager     :512  | Committing JPA transaction on EntityManager [org.hibernate.jpa.internal.EntityManagerImpl@35e880]
14:41:45,335 | DEBUG | AbstractTransactionImpl   :175  | committing
14:41:45,388 | DEBUG | JdbcTransaction           :113  | committed JDBC Connection
14:41:45,389 | DEBUG | JdbcTransaction           :126  | re-enabling autocommit
14:41:45,393 | DEBUG | JpaTransactionManager     :600  | Closing JPA EntityManager [org.hibernate.jpa.internal.EntityManagerImpl@35e880] after transaction
14:41:45,394 | DEBUG | EntityManagerFactoryUtils :432  | Closing JPA EntityManager
14:41:45,396 | DEBUG | LogicalConnectionImpl     :246  | Releasing JDBC connection
14:41:45,397 | DEBUG | LogicalConnectionImpl     :264  | Released JDBC connection

我尝试了什么:

  • Propagation.REQUIREDPropagation.REQUIRES_NEW
  • 方法调用后
  • .flush()
  • 打开子查询以进行测试。它工作正常。
  • 更新Spring Data / Hibernate

注意:

  • 出于性能原因,我未在MySecondEntity中添加MyEntityMyEntity可以有10k + MySecondEntity。我知道我可以使用延迟提取,但实际上,实体被映射到数据传输对象,这会导致映射过程出现问题。另外,我不希望检索所有数据来删除它们。
  • <tx:annotation-driven/>已定义
  • <aop:aspectj-autoproxy/>已定义
  • 该方法直接从@RestController
  • 调用

0 个答案:

没有答案