NotificationParamEntity オブジェクトのリストであるパラメーターと OneToMany 関係を持つ通知エンティティがあります。
両方のクラスのコードは次のようになります。
// 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 WHERE p.key = "P1" AND p.value = 'V1'
しかし、指定された 2 つのパラメーター (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"
うまくいかない理由は理解できます。2 つの異なるキーを持つことができるパラメーターがないため、クエリは何も返しません。
しかし、これを機能させるにはどうすればよいでしょうか。1 つは P1 という名前で値は V1、もう 1 つは P2 で値は V2 の 2 つのパラメーターを持つ通知エンティティがあるとします。JPQL クエリでこの通知エンティティを見つけるにはどうすればよいですか?