JPQL联合表

时间:2015-12-05 21:57:24

标签: java sql jpa spring-data jpql

我有一个合作者和干预表。

协作者(Id Collab,协作名称,地址协作) 干预(id_Interv,Nom_Interv,End_date,Id_collab)("实体响应包含协作者类型对象)

@Entity
public class Collaborateur implements Serializable {

   private static final long serialVersionUID = 1L;
   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   private Long idcollaborateur;
   private String nomCollaborateur;
   ...
}

@Entity
public class Intervention implements Serializable{
   private static final long serialVersionUID = 1L;

   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   private Long idInterv;
   @Temporal(TemporalType.DATE)
   private Date dateEndInterv;
   @ManyToOne(fetch = FetchType.LAZY)
   @JoinColumn(name = "idCollaborateur")
   private Collaborateur collaborateur;
   ...
 }

我如何选择所有合作者(列表)目前还没有任何干预措施.. 即没有干预的合作者或者他们所有干预的dateEndInterv小于datetimenow

我希望很明白,我的论文:

SELECT c
FROM Collaborateur c LEFT JOIN c.Intervention i
WHERE (i.dateFinIntervention<?1 or i.dateFinIntervention IS NULL)

我参与了春季数据

1 个答案:

答案 0 :(得分:0)

您需要在Collaborateur和Intervention之间建立新关系,例如@OneToMany

@Entity
public class Collaborateur implements Serializable {

   private static final long serialVersionUID = 1L;
   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   private Long idcollaborateur;
   private String nomCollaborateur;

   @OneToMany
   private Set<Intervention> interventions;
   ...
}

所以,你的JPQL将是:

SELECT c
FROM Collaborateur c 
LEFT JOIN c.interventions i
WHERE (i.dateEndInterv < ?1 OR i.dateEndInterv IS NULL)