通过Spring Data中的@Modifying @Query更新无效

时间:2015-01-27 10:42:35

标签: java hibernate spring-data

我定义了以下存储库:

@Repository
public interface IntegrationPeriodConstraintsRepository extends CrudRepository<IntegrationPeriodConstraint, IntegrationPeriod>
{
    @Query("select case when (count(ipc) > 0) then true else false end from IntegrationPeriodConstraint ipc where ipc.m_enabled = true")
    boolean hasEnabledConstraints();

    @Modifying
    @Query("update IntegrationPeriodConstraint ipc set ipc.m_lastIntegrationTimeIntegratedData = :time where ipc.m_integrationPeriod = :integrationPeriod")
    void setLastIntegrationTimeIntegratedData( @Param("integrationPeriod") IntegrationPeriod integrationPeriod,
                                               @Param("time") DateTime lastIntegrationTime );
}

但是,似乎此setLastIntegrationTimeIntegratedData方法无效。我有一个使用嵌入式H2数据库的单元/集成测试,该数据库显示在调用之后,数据库中没有更新。

我启用了Hibernate日志记录,它显示了这个:

2015-01-27 11:49:13 DEBUG [main] AnnotationTransactionAttributeSource - Adding transactional method 'IntegrationPeriodConstraintsServiceImpl.setLastIntegrationTime' with attribute: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
2015-01-27 11:49:13 DEBUG [main] AnnotationTransactionAspect - Skipping transactional joinpoint [com.company.server.common.service.message.impl.IntegrationPeriodConstraintsServiceImpl.setLastIntegrationTime] because no transaction manager has been configured
2015-01-27 11:49:13 DEBUG [main] AbstractFlushingEventListener - Processing flush-time cascades
2015-01-27 11:49:13 DEBUG [main] AbstractFlushingEventListener - Dirty checking collections
2015-01-27 11:49:13 DEBUG [main] AbstractFlushingEventListener - Flushed: 0 insertions, 0 updates, 0 deletions to 1 objects
2015-01-27 11:49:13 DEBUG [main] AbstractFlushingEventListener - Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
2015-01-27 11:49:13 DEBUG [main] EntityPrinter - Listing entities:
2015-01-27 11:49:13 DEBUG [main] EntityPrinter - com.traficon.domain.system.IntegrationPeriodConstraint{m_integrationPeriod=MINUTES_1, m_lastIntegrationTimeIntegratedData=null, m_enabled=true, m_lastIntegrationTimePresenceData=null, m_storageSingleFieldPeriod=PT6M, m_lastIntegrationTimeFlowData=null, m_lastIntegrationTimeBicycleData=null}
2015-01-27 11:49:13 DEBUG [main] SQL - update IntegrationPeriodConstraint set lastIntegrationTimeIntegratedData=? where integrationPeriod=?
2015-01-27 11:49:13 TRACE [main] BasicBinder - binding parameter [1] as [TIMESTAMP] - 2015-01-27 11:49:13.81
2015-01-27 11:49:13 DEBUG [main] JpaTransactionManager - Found thread-bound EntityManager [org.hibernate.ejb.EntityManagerImpl@5f0e6817] for JPA transaction
2015-01-27 11:49:13 DEBUG [main] JpaTransactionManager - Participating in existing transaction

我觉得奇怪的是第二个参数没有记录?

这是我使用的Hibernate映射文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.mycompany.domain.system" default-access="field">


    <class name="IntegrationPeriodConstraint" table="IntegrationPeriodConstraint">

        <id name="m_integrationPeriod" column="integrationPeriod">
            <type name="com.mycompany.persistence.impl.hibernate.usertype.EnumUserType">
                <param name="enumClass">com.mycompany.domain.message.data.IntegrationPeriod</param>
            </type>

            <generator class="assigned"/>
        </id>

        <property name="m_storageSingleFieldPeriod" not-null="true"
                  type="com.mycompany.persistence.impl.hibernate.usertype.BaseSingleFieldPeriodUserType">
            <column name="storagePeriodType"/>
            <column name="storagePeriodValue"/>
        </property>

        <property name="m_enabled" not-null="true" column="enabled"/>

        <property name="m_lastIntegrationTimeIntegratedData" column="lastIntegrationTimeIntegratedData" type="org.jadira.usertype.dateandtime.joda.PersistentDateTime"/>
        <property name="m_lastIntegrationTimeFlowData" column="lastIntegrationTimeFlowData" type="org.jadira.usertype.dateandtime.joda.PersistentDateTime"/>
        <property name="m_lastIntegrationTimePresenceData" column="lastIntegrationTimePresenceData" type="org.jadira.usertype.dateandtime.joda.PersistentDateTime"/>
        <property name="m_lastIntegrationTimeBicycleData" column="lastIntegrationTimeBicycleData" type="org.jadira.usertype.dateandtime.joda.PersistentDateTime"/>
    </class>

</hibernate-mapping>

3 个答案:

答案 0 :(得分:0)

这似乎是另一种方式

  

名称= “m_lastIntegrationTimeIntegratedData”   柱= “lastIntegrationTimeIntegratedData”

Column是数据库中列的名称,name是实体中的属性。

答案 1 :(得分:0)

用@Modifying(clearAutomatically = true)替换@Modifying应该可以。

答案 2 :(得分:0)

您可以在Spring Data JPA中进行检查(在“修改查询”下):
https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#reference

这:

https://www.baeldung.com/spring-data-jpa-modifying-annotation

这对我有用:

@Transactional
@Modifying(flushAutomatically = true)
@Query("UPDATE User a SET a.activeStatus=?1 WHERE a.username=?2")
int setUserActiveStatusFlag(int activeStatus,String username);

对于我使用的@Transactional:javax.transaction.Transactional