J is for Java
J is for Java

Reputation: 365

Spring JpaRepository doesn't update table

I have next JPARepo code:

public interface MyObjectRepository extends JpaRepository<MyObject, UUID> {
  @Modifying
  @Query("UPDATE MyObject obj SET obj.deleted = true WHERE obj.objectId = ?1")
  void deleteById(UUID uuid);
}

and Service for it's calling:

@Service
@Slf4j
@Transactional(readOnly = true)
public class MyService {
@Autowired
  private MyObjectRepository myObjectRepository;

  // some methods here

  @Transactional
  public void deleteMyObject(MyObject myObject) {
    ** first way **
    myRepository.deleteById(myObject.getMyObjectId());
    ** second way **
    myObject.setDeleted(true);
    myObjectRepository.save(myObject);
  }

  //some methods here

}

two ways of updating are executed only once. The next time I call method, entity's value at delete column is false by default and after update it stays false. But when in debug I look at the table step by step, I see that deleted column was set to true and when I release debug, delete flag is set to false again. Is it caching or incorrect annotating?

Upvotes: 0

Views: 1636

Answers (1)

PRATHAP S
PRATHAP S

Reputation: 775

I think @Transactional(readOnly = true) at the class level applies to all methods, Can you change it to @Transactional(readOnly = false) in the method level.

 @Transactional(readOnly = false)
  public void deleteMyObject(MyObject myObject) {

Upvotes: 1

Related Questions