我不想在保存父实体上选择子集合
我有父/子关系,子集合被映射为懒惰。我取一个父,当然因为延迟加载子集合没有加载。但是当我想更新我的父亲时,第一个子集合正在加载,那么父正在更新。性能问题!怎么解决 ?
Parent entity :
@Entity
@Table(name = "LOAN")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Loan implements java.io.Serializable {
private Long id;
private Set<VchN> vchNs=new HashSet<VchN>(0);
private Set<VchN2> vchNs2=new HashSet<VchN2>(0);
.
.
.
.
private Set<VchN100> vchNs100=new HashSet<VchN100>(0);
/*
id setter getter
*/
@OneToMany(fetch = FetchType.LAZY)
@JoinColumn(name = "loan_id", insertable = false,updatable = false)
public Set<VchN> getVchNs() {
return vchNs;
}
public void setVchNs(Set<VchN> vchNs) {
this.vchNs = vchNs;
}
/*
setters getters
*/
}
Child enity:
@Entity
@Table(name = "VCH_N")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class VchN implements java.io.Serializable {
private Loan loan;
@Column(name = "loan_id", nullable = false)
public Long getLoanId() {
return loanId;
}
public void setLoanId(Long loanId) {
this.loanId = loanId;
}
}
so on......
&#13;
Now when I execute
loanRepository.save(loan);
Output:
select columns from VchN where loan_id=?
select columns from VchN2 where loan_id=?
.
.
.
select columns from VchN100 where loan_id=?
update Loan set column = ?
I just want to update parent entity.
答案 0 :(得分:0)
尝试双向关系。在VchN实体上,在Loan属性上指定@ManyToOne批注。删除Loan实体中每个VchN集合属性的@JoinColumn批注。而是将@JoinColumn注释放在VchN实体类的loan属性上。通过使用@OneToMany批注的mappedby属性,指定Loan实体是关系的反面。
以下示例:
public class Loan implements java.io.Serializable {
...
@OneToMany(fetch = FetchType.LAZY, mappedBy="loan")
public Set<VchN> getVchNs() {
return vchNs;
}
...
public class VchN implements java.io.Serializable {
...
@ManyToOne
@JoinColumn(name = "loan_id")
public Loan getLoan() {
return loan;
}
...