【问题标题】:JPQL Query ManyToOneJPQL 查询多对一
【发布时间】:2013-06-11 04:33:09
【问题描述】:

ManyToOne 单向和双向关系的 JPQL 查询


    class ParentEntity{
      @Id
      String parentId1; //composite key
      @Id
      String parentId2;
      @ManyToOne
      ChildEntity childEntityRef;
    }

    class ChildEntity {  //ManyToOne Unidirectional Relationship case
      @Id
      String childId1;  //composite key
      @Id
      String childId2;
    }
    or
    class ChildEntity {  //ManyToOne Bidirectional Relationship case
      @Id
      String childId1;  //composite key
      @Id
      String childId2;
      @OneToMany
      List<ParentEntity> parentEntitiesref;
    }

_______________________________________________________________________
||        ParentEntity  Table              ||    ChildEntity Table     ||
||_________________________________________||__________________________||
parentId1|parentId2|childId1_FK|childId1_FK|| childId1| childId2       ||
||_______|_________|___________|___________||_________|________________||
|| p11   |p21      | c11       |c21        ||   c11   |    c21         ||
|| p12   |p22      | c11       |c21        ||   c12   |    c22         ||
|| p13   |p23      | c12       |c22        ||   c13   |    c23         ||
|| p14   |p24      | c12       |c22        ||         |                ||
||_______|_________|___________|___________||_________|________________||

我需要 JPQL 查询来访问未连接到特定 ParentEntity 的所有 childEntity 的列表(在这两种情况下都是单向和双向关系)。

Example -
For ParentEntity[p11,p21] get All other disconnected List of ChildEntity([c12,c22],[c13,c23])

最好的问候,
高拉夫·古普塔。

【问题讨论】:

    标签: sql jpa jpa-2.0 eclipselink jpql


    【解决方案1】:
    select c from ChildEntity c where c.childId != (
        select p.childEntityRef.childId from ParentEntity p where p.parentId = :parentId)
    

    【讨论】:

    • 感谢您的回复,我已经更新了我对复合键实体的问题,请您在复合键案例中帮助我
    • Reloved with query "select c from ChildEntity c where c != ( select p.childEntityRef from ParentEntity p where p.parentId = :parentId)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-06
    • 1970-01-01
    • 2017-11-17
    • 2011-07-29
    • 2012-06-08
    • 2020-03-26
    • 2013-06-22
    相关资源
    最近更新 更多