如何构建实体X&在这种情况下它与P的关系?和注释使用?
实体P,它有1个PT和xs(x列表)。
@Entity
public class P {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@OneToOne
@JoinColumn(name = "pt_id")
private PT pt;
@OneToMany
private List<X> xs;
...
}
@Entity
public class PT {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;
}
xs是基于PTST(ST&amp; PT的链接表)的过滤ST列表, 并且左连接存储在PV中的P值。
表PV,它有p_id,st_id,val。
示例:
P:
id pt_id
1 1
2 2
3 1
PT:
id name
1 pt-1
2 pt-2
ST
id t
1 A
2 B
3 C
4 D
5 E
PTST
pt_id st_id
1 1
1 3
2 1
2 2
PV
p_id st_id val
1 1 1a
2 2 2b
预期产出:
P = 1
p_id st_id st_t val
1 1 A 1a
1 3 C NULL
P = 2
p_id st_id st_t val
2 1 A NULL
2 2 B 2b
目前我正在使用以下查询来获取数据
select st.id as st_id, st.t, :p2 as p_id, pv.val
from PTST ptst
inner join ST st on (st.id = ptst.st_id)
left join PV pv on (pv.p_id = :p1 and pv.st_id = ptst.st_id)
where PTST.pt_id = :p3
然后输出
p_id: 1, st_id: 1 t: A val: 1a
p_id: 1, st_id: 3 t: C val: NULL
任何建议?谢谢。
更新
上面的查询是手动检索我想要自动化的不洁实体X的数据(不再是原生查询)。
UPDATE2:
@Entity
@Table(name = "PV")
public class X{
@EmbeddedId
private Y id = new Y();
@Column(updatable = false, insertable = false)
private String st_t;
private String val;
}
@Embeddable
public class Y implements Serializable {
@Column(updatable = false)
private int p_id;
@Column(updatable = false)
private int st_id;
}
答案 0 :(得分:0)