这就像 groovy 和 Java 的混合。作为学习的一部分,我创建了代码。
===================================================================
@Entity
public class GroovyBoy implements SimpleEntity {
@Id
@GeneratedValue
private long Id;
private String name;
@OneToMany(fetch=FetchType.EAGER,cascade=CascadeType.ALL)
private List<GroovyBro> brothers;
@Override
public Long getId() {
return Id;
}
@Override
public void setId(Long id) {
Id = id;
}
}
===================================================================
@Entity
class GroovyBro implements SimpleEntity {
@Id
@GeneratedValue
private long Id;
private String name;
@ManyToOne(fetch=FetchType.EAGER)
private GroovyBoy brother;
@Override
public Long getId() {
return Id;
}
@Override
public void setId(Long id) {
Id = id
}
}
===================================================================
def dao = (MyDaoImpl) appContext.getBean("dao")
def boy = new GroovyBoy()
boy.name='boy1'
def bro1 = new GroovyBro()
bro1.name='bro1'
def bro2 = new GroovyBro()
bro2.name='bro2'
boy.brothers = [bro1, bro2]
dao.save(boy)
//dao.evict(boy)
println 'Id: ' + boy.brothers[0].id
def loadBro = new GroovyBro();
loadBro.id = boy.brothers[0].id
GroovyBro bro = (GroovyBro) dao.get(loadBro)
println 'Bro: ' + bro.brother
===================================================================
最后一行返回 null。但是当我获取 GroovyBoy 实体时,它会加载持久包,然后加载 GroovyBro 实例。为什么没有加载 GroovyBoy 实例?
提前致谢。