0

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 クエリでこの通知エンティティを見つけるにはどうすればよいですか?

4

1 に答える 1

1

次のようなことを試してください:

SELECT n FROM NotificationEntity n WHERE EXISTS 
     (SELECT p FROM NotificationParamEntity p WHERE p.key = 'P1' AND p.value = 'V1' 
     AND p.notificationEntity = n) 
AND EXISTS 
     (SELECT p2 FROM NotificationParamEntity p2 WHERE p2.key = 'P2' AND p2.value = 'V2' 
     AND p2.notificationEntity = n)

NotificationParamEntity から NotificationEntity への参照が必要であることに注意してください (コードのスニペットにその列は表示されませんが、@ManyToOne が必要です)。

于 2012-07-25T13:41:40.090 に答える