JPQL相当于带有EXISTS和关系的SQL查询

时间:2012-07-05 16:18:47

标签: playframework jpql

我正在使用Play Framwork 1.2.4

我创建了3个带有关系的Model类:

标题< 1 - n>第1行> LineType

@Entity
public class Header extends Model {
   @OneToMany(mappedBy="header" , cascade=CascadeType.ALL)
   public List<Line> lines;
   ...
}

@Entity
public class Line extends Model{
   @ManyToOne
   public Header header ;

  @ManyToOne
   publicLineType lineType;
   ...
}

@Entity
public class LineType extends Model {

   public Integer code ;
   ...
}

我想在标题搜索期间对线型进行过滤。

如何在Play Framework中使用JPQL(或其他)编写此SQL查询的等效内容?

 Select * from header where exists(select 1 from line,typeline where line.header=header.id and  line.lineType=linetype.id and linetype.code = 'X')

1 个答案:

答案 0 :(得分:1)

我回答我自己的问题:

Query query = JPA.em().createQuery("select header from Header as header where exists(select line from Line line JOIN ligne.lineType as type where ligne.header=header and type.code='X')");
List<Header> headers = query.getResultList();

内部联接:http://docs.oracle.com/html/E24396_01/ejb3_langref.html#ejb3_langref_inner_joins

存在:http://docs.oracle.com/html/E24396_01/ejb3_langref.html#ejb3_langref_exists