其实在定义单向关系中只有三情况:
OneToOne
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name="当前对象的关联id")
使用如:
1.直接使用
JPQL:select o.id, o.name,o.relaObj.id,o.relaObj.name from User o where o.id=?1,
此种方式生成的SQL:select t0.id,t0.name,t1.id,t2.name from User t0,Rel t1 where t0.id=?1 and t0.当前对象的关联id=t1.id
2.用join
JPQL:select o.id, o.name,r.id,o.r.name from User o left join o.relaObj r where o.id=?1
第2种方式常用一些
OneToMany
@OneToMany(cascade=CascadeType.ALL,fetch=FetchType.EAGER)
@JoinTable(
joinColumns = { @JoinColumn( name="condition_id") },//当前对象的关联id
inverseJoinColumns = @JoinColumn( name="actionDefine_id")//关联表的id
)
@IndexColumn(name="sortFlag")
ManyToMany
@ManyToMany(cascade=CascadeType.ALL,fetch=FetchType.EAGER)
@JoinTable(
name="中间表",,
joinColumns = { @JoinColumn( name="condition_id") },//当前对象的关联id
inverseJoinColumns = @JoinColumn( name="actionDefine_id")//关联表的id
)
@IndexColumn(name="sortFlag")