我有以下课程:
@Entity
public class ClassA {
@Column
@Id
private Long id;
@ManyToOne(cascade = javax.persistence.CascadeType.ALL)
@JoinColumn...
private ClassB classB;
public ClassA(ClassB classB){
this.classB = classB;
}
//....
//....
}
@Entity
public class ClassB {
@Column
@Id
private Long id;
@Column(unique=true) //important
private String name;
public ClassB(String name){
this.name = name;
}
//....
//....
}
我想保存ClassA
并ClassB
仅在ClassB
尚不存在时保存。
ClassB b = new ClassB("myName");
ClassA a = new ClassA(b);
session.merge(a); // fine
ClassA a2 = new ClassA(b);
session.merge(a2); // problem with b
当然,我可以检查ClassB
它是否存在并加载它,但我想提高效率。