JPQL查询ManyToOne

时间:2013-06-14 13:44:36

标签: sql jpa jpa-2.0 eclipselink jpql

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的List(在单向和双向关系中)。

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

最诚挚的问候,
Gaurav Gupta。

1 个答案:

答案 0 :(得分:0)

select c from ChildEntity c where c.childId != (
    select p.childEntityRef.childId from ParentEntity p where p.parentId = :parentId)