Spring Data JPA Update @Query not updating?
The EntityManager doesn't flush change automatically by default. You should use the following option with your statement of query:
@Modifying(clearAutomatically = true)
@Query("update RssFeedEntry feedEntry set feedEntry.read =:isRead where feedEntry.id =:entryId")
void markEntryAsRead(@Param("entryId") Long rssFeedEntryId, @Param("isRead") boolean isRead);
JPA update not updating field
As far as I understand from the documentation, @Modifying
only changes how the query is executed so that updates actually work. But it does not by default ensure that the persistence context is updated correctly.
In your case, the entity is already loaded into the persistence context (by calling paymentTransactionRepository.save
) and then cancelTransaction
updates the database (but not the entity in the persistence context!). But the following findBySiteIdAndTransactionNumber
query fetches the entity from the persistence context which is outdated at that point.
To fix that, change the annotation to this:
@Modifying(clearAutomatically = true, flushAutomatically = true)
clearAutomatically
is the important part here because it clears the persistence context and therefore ensures that the next query cannot fetch the outdated entity from it.
flushAutomatically
would only be needed if you changed the PaymentTransaction
entity prior to the cancelTransaction
call. Without the flush, those changes would be lost (because the changed entity gets cleared from the context). It might actually required in your case because the paymentTransactionRepository.save
might not be flushed yet otherwise.
Also: This all happens because both the creation, cancelling and reading of paymentTransaction
happen inside the same transaction. That is because @DataJpaTest
includes @Transactional
which means the entire test method runs in a single transaction.
Spring Data JPA update query (Not supported for DML operations)
You need to set the return type of your method to void
or int
.
Update of Date fields from Parent class not working in Spring Data Jpa
In order to make auditing work at all, please insure @EnableJpaAuditing
annotation added to @Configuration
class.
The AuditingEntityListener
methods are called in the @PrePersist
and @PreUpdate
phase. AFAIK is works only if you directly manipulate entities. It doesn't work if you use @Query
statements.
Update statement must do it manually, for example:
UPDATE Employee p SET p.status =:status, p.lastUpdatedOn =
:lastUpdatedOn, p.lastUpdatedBy = :lastUpdatedBy WHERE
p.srcProjectKeyId = (:id)
To get the current date use:
Date now = new Date();
// or
long now = System.currentTimeMillis();
To get the current user (a.k.a. principal) use:
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
String currentPrincipalName = authentication.getName();
Here are longer discussions about cache invalidation issues:
Spring Boot Data JPA - Modifying update query - Refresh persistence context
Spring Data JPA Update @Query not updating?
Clear Hibernate 2nd level cache after manually DB update
Spring Boot JPA @Query Update does not work
@Modifying(clearAutomatically = true, flushAutomatically = true)
@Transactional
@Query(value = "update pojazd set czy_zarchiwizowany = true
where id = :selected_id", nativeQuery = true)
fun archwizujPojazd(@Param(value="selected_id") Long selected_id);
Related Topics
Converting an Array of Objects to an Array of Their Primitive Types
Issue Using Imageio.Write Jpg File: Pink Background
What Is the Purpose of @Namedarg Annotation in Javafx 8
Java 8: Difference Between Method Reference Bound Receiver and Unbound Receiver
"Unable to Acquire Application Service" Error While Launching Eclipse
How to Pass Arithmetic Operators to a Method in Java
Java Inetaddress.Getlocalhost(); Returns 127.0.0.1 ... How to Get Real Ip
How to Obtain Mouse Click Coordinates Outside My Window in Java
How to Calculate a Time Span in Java and Format the Output
How to Retrieve Value from Jtextfield in Java Swing
Eclipse: Attach Source/Javadoc to a Library via a Local Property
How to Access Resources in Jar File
Java Swing: How to Change Gui Dynamically
How to Get Information About the Local Variables Using Java Reflection