質問

次のサブクエリを変換して、冬眠サブクエリを使用したいと思います。

getCurrentSession().createQuery("from Employee where id in (select adminId from Department where adminId is not null)")
                   .list();
  • 従業員:

    @ManyToOne
    @JoinColumn(name = "fk_department_id", nullable = true) 
    private Department department;
    
  • デパートメント:

    @OneToMany(fetch = FetchType.EAGER)
    @JoinColumn(name = "fk_department_id")
    private Set<Employee> employees = new HashSet<Employee>(0);
    

私はいくつかの例を読んでいて、それでもそれを行う方法を理解できないので、誰でも私にこの改宗者の例を提供してください。

役に立ちましたか?

解決

Criteria c = session.createCriteria(Employee.class, "e");
DetachedCriteria dc = DetachedCriteria.forClass(Departemt.class, "d");
dc.add(Restrictions.isNotNull("d.adminId");
dc.setProjection(Projections.property("d.adminId"));
c.add(Subqueries.propertyIn("e.id", dc));

setProjection 通話はサブクエリを返します adminId 全体の代わりにのみプロパティ Department 実在物。 Subqueries.propertyIn 制限を作成します:プロパティ id 検索された従業員のものはそうでなければなりません in サブクエリによって返された結果のセット。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top