我正在使用Hibernate並試圖在我的實體中爲SELECT
查詢構建下一個邏輯。創建一個連接列,如果教授的值爲name = NULL
,則選擇教師姓名的值。當值爲1時,Hibernate實體連接替換值2 NULL
規範教師表:
@Entity
@Table(name = "teacher")
public class Teacher {
@Id
@Column(name = "id_number)
private String id;
@OneToOne
@JoinColumn(name = "t_name")
private Professor name;
// Getters and Setters ...
}
代碼教授表:
@Entity
@Table(name = "professor")
public class Professor{
@Id
@Column(name = "id_number)
private String id;
@Column(name = "p_name")
private String name;
// Getters and Setters ...
}
工作SQL查詢例子:
select
t.id_number as "Identification Number",
isnull(p.p_name, t.t_name) as "Name"
from teacher t
left join professor p
on t.t_name = p.p_name
where id_number in (23, 24, 25, 26, 27)
我應該在我的實體更改複製邏輯以上的SQL查詢?非常感謝您提供的任何幫助。
謝謝你的幫助。我發現第二個解決方案很有用。 – Edster