标准休眠加入

时间:2019-02-19 10:52:18

标签: java hibernate criteria

我需要执行一个返回“ b”对象列表的条件。我有以下课程:

---
A
---
private BigDecimal id;
private String name;
private B b;

---
B
---
private BigDecimal id;
private String name;

我这样做:

Criteria criteria = getSession().createCriteria(A.class,"a").createCriteria("b");
return criteria.list();

我总是得到一个列表,我不知道如何得到列表。有可能吗?

编辑:使用Java SE 1.7

3 个答案:

答案 0 :(得分:1)

如果您不希望A和B之间存在双向关系,请使用别名,然后仅收集b属性。

List<A> aList = getSession().createCriteria(A.class, "a")
        .createAlias("a.b", "b")
        .add(Restrictions.eq("a.name", "A name")
        .add(Restrictions.eq("b.name", "B name")
        .list();
List<B> bList = new ArrayList<>(aList.size());
for (A a : aList) {
    bList.add(a.getB());
}

答案 1 :(得分:1)

您可以使用以下嵌套条件来导航到关联:

List<B> result = session.createCriteria( A.class )
     .add( Restrictions.like("name","he%") )
     .createCriteria( "b" )
         .add( Restrictions.like("name", "he%") )
     .list();

我的“第二”答案不是针对标准,而是针对CriteriaBuilder,对于小用例,我不建议这样做,但对于需要根据用户或其他人的变化条件动态创建查询的用例,我不建议这样做。应用程序。

final CriteriaBuilder queryBuilder = getCriteriaBuilder(); // Retrieve your CriteriaBuilder here. I inject mine over CDI for example...
final CriteriaQuery<B> query = queryBuilder.createQuery( B.class ); // Type the query to it's expected end result type.

final Root<A> queryRoot = query.from( A.class ); // Select the root of the query
final Join<Object, Object> BJoin= queryRoot.join("b", JoinType.LEFT ); // "b" is the field name to use for the mapping between the root table to the joined table. In this case a.b
// The above equals "left join b on b.id = a.b.id "

// Perform a select with the Class resulting from the select and what wants to be selected. 
// It is also possible to select only a field of a table but in our case we want the whole table of B to be selected.
final Selection<B> select = queryBuilder.construct( B.class, BJoin ); 
query.select(select); // add the select to the query

// We need to remember the ParameterExpression in order to fill the where condition.
// This acts as a typed(!) blank to later fill with the condition we want to match
final ParameterExpression<String> bName = queryBuilder.parameter(String.class);

// Define the where condition using the Path<T> you retrieve from Root or Join objects.
// This will make hibernate build the condition for the correct table like b.name
final Predicate bPredicate = queryBuilder.equal( bJoin.get("name"),bName );

query.where(bPredicate); // add the where expression to the query.
// The above equals something like "where b.name = ?"

// Compile the built query to a TypedQuery
// The EntitiyManager is also injected over CDI in my usual case.
final TypedQuery<B> builtQuery = javax.persistence.EntityManager.createQuery(query);
builtQuery.setParameter(bName,"test"); // Fill in the ? of the where condition.

final List<B> resultList = builtQuery.getResultList();

一开始这看起来很繁琐,但可以非常动态地使用它,因为您可以从片段中提取多种方法,并可以添加多个where条件,order by,group by等。

答案 2 :(得分:0)

最后我做到了:

public List<B> findXXXX(A a) {

    // A
    DetachedCriteria aCriteria = DetachedCriteria.forClass(A.class, "a");
    aCriteria.add(Restrictions.eq("a.name", "some name"));
    aCriteria.setProjection(Projections.property("a.key"));

    // B
    Criteria bcriteria = getSession().createCriteria(B.class, "b");
    bcriteria.add(Property.forName("a.key").in(aCriteria));

    return bcriteria.list();
}

一个子查询,因为它将在数据库中记录很少的记录。 感谢您的帮助!