I am struggling with this..
Whenever I traverse a collection inside a transaction it will trigger an SQL update for one of its field.
@Entity
class A {
@ManyToOne
@JoinColumn(name = "b1_id")
private B b1;
@ManyToOne
@JoinColumn(name = "b2_id")
private B b2;
}
Unidirectional so B knows nothing about A.
@org.springframework.transaction.annotation.Transactional //1. do nothing
private void doSomething(){
List<A> aEntities = aRepository.findAll();
}
@org.springframework.transaction.annotation.Transactional //2.traversing the list
private void doSomething(){
List<A> aEntities = aRepository.findAll();
for (A a: aEntities){
//dont do anything
}
}
private void doSomething(){ // 3.no transaction
List<A> aEntities = aRepository.findAll();
for (A a: aEntities){
//dont do anything
}
}
SQL log shows:
1. only SELECT is generated
2. UPDATE for b1 (but not for b2) !!!!!
3. only SELECT is generated
And on the top of that 2 UPDATE will be run for b1.
If I turn on JPA versioning I can see that b1's version is incremented by 2, b2 left untouched.(If I remove b2, the same update will be launched for b1)
This drives my nuts....
The class B is unimportant imo because for the other instance of it no update will be generated.
And I have several other classes with manytoany assocciations but never encountered such an issue.
This situation comes up only for b1 in entity A.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…