Wim Deblauwe
Wim Deblauwe

Reputation: 26858

Update via @Modifying @Query in Spring Data not working

I have the following repository defined:

@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 );
}

However, it seems that this setLastIntegrationTimeIntegratedData method is not working. I have a unit/integration test using an embedded H2 database that shows that after the call, there is no update in the database.

I enabled Hibernate logging and it shows this:

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

What I find strange is that there is no logging for the 2nd parameter?

This is the Hibernate mapping file I use:

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

Upvotes: 1

Views: 8045

Answers (5)

Reema
Reema

Reputation: 80

to whoever coming for similar issue, for my case the table name was wrong and I was updating another table, so first thing run the query in the db client and check its validity. another debugging option is using sql-show command so it will print hibernate queries in the console.

Upvotes: 0

Ayomirotoye
Ayomirotoye

Reputation: 1

Doing this way could fix the problem as it fixed mine

@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 );

Upvotes: 0

Winter MC
Winter MC

Reputation: 375

You can Check this for Spring Data JPA (under Modifying Queries) :
https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#reference

and this:

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

This is what was working for me:

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

for the @Transactional i used : javax.transaction.Transactional

Upvotes: 0

Pruthviraj
Pruthviraj

Reputation: 578

Replacing @Modifying with @Modifying(clearAutomatically = true) should work.

Upvotes: 3

Cristian Sevescu
Cristian Sevescu

Reputation: 1489

This seem to be the other way around than how it should

name="m_lastIntegrationTimeIntegratedData" column="lastIntegrationTimeIntegratedData"

Column is the name of the column in the database and name is the property in the entity.

Upvotes: 0

Related Questions