【问题标题】:Nested collection using Criteria: How to retrieve table1.getTable2().get(0) using criteria使用条件的嵌套集合:如何使用条件检索 table1.getTable2().get(0)
【发布时间】:2017-06-02 13:29:18
【问题描述】:

我有 2 个表 TABLE1 和 TABLE2。 TABLE1 的休眠类

类 Table1 {

私有集合 table2s= new ArrayList();

}

现在使用标准我需要获取 Table1 记录,结果应该在链接到 Table1 的列表中包含 Table2s 一条记录。

例如:表 1 表 1ID 名称 1个 2乙 表 2 TABLE2ID TABLE1ID TABLE2NAME 1 1 XXXX 2 1 XXXXXX 3 1 XXX 4 2 年年年 5 2 年年年年年

预期输出: TABLE1ID 名称 TABLE2ID TABLE2NAME 1 A 1 XXXX 2 B 4 年年年

类似于 table1.gettable2s().get(0)

【问题讨论】:

    标签: hibernate orm criteria hibernate-mapping nhibernate-projections


    【解决方案1】:

    使用 join() 方法

        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<Table1> q = cb.createQuery(Table1.class);
        Root<Table1> r = q.from(Table2.class);
    
        Join<Table2, Table1> usersJoin = r.join("field", JoinType.LEFT);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-20
      • 1970-01-01
      • 1970-01-01
      • 2017-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多