简体   繁体   English

使用DetachedCriteria休眠多个子查询

[英]Hibernate multiple subqueries with DetachedCriteria

I would like write this query into HQL : 我想将此查询写入HQL:

select DISTINCT * from transportation transp 
    inner join price p on p.transportationId = transp.transportationId 
    where p.sectionId = ( select sec.sectionId from section sec where sec.lineId = ( select l.lineId from line l where l.lineId = 1000000000) )

But don't know write subqueries. 但是不知道写子查询。 I know I must use DetachedCriteria. 我知道我必须使用DetachedCriteria。

An other question: If we can write this query in native query (using createSQLQuery), how can cast returned objet to Transportation entity ? 另一个问题:如果我们可以使用本机查询(使用createSQLQuery)编写此查询,那么如何将返回的对象转换为运输实体?

Thanks 谢谢

My entities : 我的实体:

@Entity
@Table(name = "transportation")
public class Transportation implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "transportationId")
    private Integer id;

    @OneToMany(mappedBy = "transportation", fetch = FetchType.EAGER)
    @JsonBackReference(value = "price-transportation")
    private Set<Price> prices = new HashSet<Price>();
    ...
}

@Entity
@Table(name = "price")
public class Price implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "priceId")
    private Integer id;

    @ManyToOne(optional = false)
    @JoinColumn(name = "transportationId")
    @JsonManagedReference(value = "price-transportation")
    private Transportation transportation;
    ...
}

@Entity
@Table(name = "section")
public class Section implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "sectionId")
    private Integer id;

    @ManyToOne(optional = false)
    @JoinColumn(name = "lineId")
    @JsonManagedReference
    private Line line;
    ...
}


@Entity
@Table(name = "line")
public class Line implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "lineId")
    private Integer id;

    @OneToMany(mappedBy = "line", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JsonBackReference
    private Set<Section> sections = new HashSet<Section>();
    ...
}

I solved my issue by using .addEntity(Transportation.class) like this : 我通过使用.addEntity(Transportation.class)这样解决了我的问题:

String query = "select DISTINCT * from moyen_transport transp"
                + " join prix_voyage p on p.ID_MoyenTransport = transp.ID_MoyenTransport"
                + " where p.ID_Troncon = ( select t.ID_Troncon from troncon t where t.ID_Troncon = ( select l.ID_Ligne from ligne l where l.ID_Ligne = 1000000000) )";
Query result = getSession().createSQLQuery(query).addEntity(Transportation.class)**;
List<Transportation> results = result.list();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM