慕尼黑5688855
2022-11-30 16:57:35
我正在嘗試Raadpleging從我的數(shù)據(jù)庫中返回對象。這些對象包含 aPersoon和 a Les。我的目標(biāo)是返回所有 Raadplegingen,其中 Les 的 ID 等于參數(shù)。我不確定如何在我的查詢中執(zhí)行此操作。我也不太確定我的第一個(gè) namedQuery 是否也會(huì)返回 Persoon 和 Les 屬性,或者我是否需要一個(gè)連接。正如您在下面看到的,我嘗試使用這樣的參數(shù)WHERE r.les = :lesId,我認(rèn)為這是不正確的。@Entity@Access(AccessType.PROPERTY)@Table(name = "Raadplegingen")@NamedQueries({ @NamedQuery(name = "ClubRaadpleging.getAlleRaadplegingen", query = "SELECT r FROM ClubRaadpleging r"), @NamedQuery(name = "ClubRaadpleging.findByLes", query = "SELECT r FROM ClubRaadpleging r JOIN ClubLes l ON l.id =r.les.id WHERE r.les = :lesId")})public class ClubRaadpleging implements Serializable { private int id; private ClubLes les; private ClubPersoon persoon; private Date datum; public ClubRaadpleging() { } @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "RaadplegingId") public int getId() { return id; } public void setId(int id) { this.id = id; } @ManyToOne(cascade = CascadeType.PERSIST) @JoinColumn(name = "LesId") public ClubLes getLes() { return les; } public void setLes(ClubLes les) { this.les = les; } @ManyToOne(cascade = CascadeType.PERSIST) @JoinColumn(name = "PersoonId") public ClubPersoon getPersoon() { return persoon; } public void setPersoon(ClubPersoon persoon) { this.persoon = persoon; } @Temporal(TemporalType.DATE) public Date getDatum() { return datum; } public void setDatum(Date datum) { this.datum = datum; }}
1 回答

慕工程0101907
TA貢獻(xiàn)1887條經(jīng)驗(yàn) 獲得超5個(gè)贊
ClubRaadpleging和之間不需要加入ClubLes。根據(jù)實(shí)體映射,JPA 本身已經(jīng)完成了這項(xiàng)工作。
您可以嘗試以這種方式簡化 NamedQuery:
@Entity
@Access(AccessType.PROPERTY)
@Table(name = "Raadplegingen")
@NamedQueries({
@NamedQuery(name = "ClubRaadpleging.getAlleRaadplegingen",
query = "SELECT r FROM ClubRaadpleging r"),
@NamedQuery(name = "ClubRaadpleging.findByLes",
query = "SELECT r FROM ClubRaadpleging r WHERE r.les.id = :lesId")
})
public class ClubRaadpleging implements Serializable {
添加回答
舉報(bào)
0/150
提交
取消