Reputation: 3100
Using Hibernate, given a many-to-many relationship between entities Payment and Refund and usgin optimistic locking with @Version
@Entity
class Payment {
@Id
@GeneratedValue
public Long paymentId;
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<Refund> refunds = new ArrayList<Refund>();
@Version
private long version;
}
@Entity
public class DRefund {
@Id
@GeneratedValue
private Long refundId;
@ManyToMany(mappedBy = "refunds")
private List<Payment> payments = new ArrayList<Payment>();
@Version
private long version;
}
If I load a Payment
from database using Hibernate session add a Refund
to it and then save the Refund
using Hibernate session, the Payment
row in db gets updated too:
insert into Refund (version, refundId) values (?, ?, ?)
update Payment set version=? where paymentId=? and version=?
insert into Payment_Refund (payments_paymentId, refunds_refundId) values (?, ?)
The Payment
has not been changed in any way. Is this necessary or can the hibernate mappings be tweaked to avoid this behaviour? If I remove @Version
, the Payment
is not updated.
Thanks for any help!
Upvotes: 0
Views: 409
Reputation: 242786
This behavior can be configured using @OptimisticLock
annotation:
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@OptimisticLock(excluded = true)
private List<Refund> refunds = new ArrayList<Refund>();
Upvotes: 2