hibernate 通过在Spring Data 中的@修改 @query 进行更新不起作用

monwx1rj  于 2022-11-14  发布在  Spring
关注(0)|答案(5)|浏览(177)

我定义了以下存储库:

@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数据库进行了单元/集成测试,该测试显示在调用之后,数据库中没有更新。
我启用了休眠日志记录,结果显示如下:

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

我觉得奇怪的是,第二个参数没有日志记录?
这是我使用的HibernateMap文件:

<?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>
x4shl7ld

x4shl7ld1#

将@Modify替换为@Modiving(leararAutomatic=True)应该可以工作。

6mw9ycah

6mw9ycah2#

这似乎与它应该的情况相反。
Name=“m_lastIntegrationTimeIntegratedData”Column=“lastIntegrationTimeIntegratedData”
Column是数据库中的列的名称,而Name是实体中的属性。

hyrbngr7

hyrbngr73#

您可以为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);

对于我使用的@Transaction:javax.Transaction.Transaction

hxzsmxv2

hxzsmxv24#

这样做可以解决问题,就像解决我的问题一样

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

qeeaahzv5#

对于任何遇到类似问题的人,对于我的情况,表名是错误的,我正在更新另一个表,所以首先要做的是在数据库客户端运行查询并检查其有效性。另一个调试选项是使用SQL-show命令,这样它就可以在控制台中打印Hibernate查询。

相关问题