我有一個與它的參數具有OneToMany關係的通知實體,它是一個NotificationParamEntity對象的列表。如何使用同一集合的多個成員編寫JPQL查詢?
兩個類的代碼如下所示:
// Notification Entity
@Entity
@Table (name = "NOTIFICATIONS")
public class NotificationEntity {
......
@OneToMany (mappedBy = "notification")
private List<NotificationParamEntity> params;
......
}
// Notification Parameter Entity
@Entity
@Table (name = "NOTIFICATIONS_PARAM")
public class NotificationParamEntity {
......
@Column (name = "KEY", length = 40, nullable = false)
@Enumerated (EnumType.STRING)
private NotificationParameterEnum key;
@Column (name = "VALUE", length = 4000, nullable = false)
private String value;
@ManyToOne
@JoinColumn (name = "NOTIFICATION_ID", nullable = false)
private NotificationEntity notification;
......
}
現在我可以用下面的查詢來獲取一個名爲「P1」參數,用值「V1」的通知:
SELECT DISTINCT anEntity FROM NotificationEntity anEntity,IN (anEntity.params)p其中p.key = 「P1」 AND p.value = 'V1'
但是,當我想了解的是有兩個指定的參數的通知(P1 = V1和P2 = V2),我的查詢下面沒有找到任何東西:
SELECT DISTINCT anEntity FROM NotificationEntity anEntity,IN (anEntity。 params)p WHERE p.key =「P1」AND p.value ='V1'AND p.key =「P2」AND p.value =「V2」
我可以理解爲什麼它不起作用:沒有參數可以有兩個不同的鍵,所以查詢什麼都不返回。
但如何使這項工作?假設我有一個通知實體,它有兩個參數,一個名爲P1,值爲V1,另一個是P2,值爲V2,如何通過JPQL查詢找到此通知實體?
謝謝,使用子查詢可以做到這一點。你是對的,NotificationParamEntity具有對NotificationEntity的引用,我只是編輯問題並添加它。 – Yun 2012-07-25 14:54:16