UnnameDSoS
UnnameDSoS

Reputation: 153

Mandatory Flush for a repository

Situation: JPA, SpringBoot, Hibernate

public interface ViewRepository
        extends JpaRepository<SomeView, Long>, JpaSpecificationExecutor<SomeView> {

    Optional<SomeView> findByIdAndLanguageId(Long id,Long lid);
}
//service
SomeView getSomeView(){
SomeView someView1 = repo.findByIdAndLanguageId(id,lid1);
....
//now get in it in different lang id
SomeView someView2 = repo.findByIdAndLanguageId(id,lid2);

//the problem here is that the value for someView2is the same as someView1 since hibernate cash this.
}

Is there an annotation / way to prevent this caching for any call to this repository only?(not application wide turning of the caching) at service level or repository level ....

Upvotes: 0

Views: 85

Answers (2)

Christian Beikov
Christian Beikov

Reputation: 16430

If you don't want this caching to happen, you can immediately detach the entity after reading it i.e.:

SomeView someView1 = repo.findByIdAndLanguageId(id,lid1);
entityManager.detach(someView1);

Upvotes: 0

talex
talex

Reputation: 20544

This is main feature of Hibernate.

If you look something up and change new found entity, changes will be saved to database without any additional code.

If you don't want that you need to use thing called "stateless session". But please warn everyone around about it, because otherwise you end up with many surprised people. This "stateless session" isn't very popular thing and no one will expect you use it.

Upvotes: 1

Related Questions