我有一个要求,我需要在父实体(客户)的名称和状态以及子实体Order的名称属性上应用搜索关键字。

List<Criterion> aliasRestrictionsCriterion = new ArrayList<Criterion>();
Junction quotedLikeRestrictions = Restrictions.disjunction();
quotedLikeRestrictions.add(Restrictions.ilike("customerName", token, MatchMode.ANYWHERE));
quotedLikeRestrictions.add(Restrictions.ilike("customerState", token, MatchMode.ANYWHERE));
restrictions.add(quotedLikeRestrictions);


并在查询中包含子表,我正在执行以下操作

Criteria alias = criteria.createAlias("order", "order")
.add(Restrictions.ilike("order.name", token, MatchMode.ANYWHERE));


Hibernate正在生成以下查询,但我需要将诸如order.name这样的比较添加到父块中。

CUSTOMER this_ inner join ORDER order_ on this_.ORDER_ID=order_.ORDER_ID where
(lcase(this_.name) like ? or lcase(this_.state) like ?) and (lcase(order_.NAME)
like ?)


我想要的是将(lcase(order_.NAME)像?)放在父块中。所以我想要的是以下

(lcase(this_.name)如?或lcase(this_.state)如?或lcase(order_.NAME)如?)

我该如何实现?

更新:这就是我所说的标准

protected List getEntities(List<Criterion> restrictions, String aliasSearchToken) {
    Session session = currentSession();
    Criteria criteria = session.createCriteria(getEntityClass());

    // apply restrictions.
    if (restrictions != null) {
        for (Criterion criterion : restrictions) {
            criteria.add(criterion);
        }
    }

    Criteria alias = criteria.createAlias("order", "order").add(Restrictions.ilike("order.name", searchToken, MatchMode.ANYWHERE));

    List list = criteria.list();
    if (list != null) {
        return list;
    }
    return Collections.emptyList();
}


更新2:

protected List getEntities(List<Criterion> restrictions, String aliasSearchToken) {
    Session session = currentSession();
    Criteria criteria = session.createCriteria(getEntityClass());

    Junction or = Restrictions.disjunction();
    or.add(Restrictions.ilike("order.name", searchToken, MatchMode.ANYWHERE));
    criteria.createAlias("order", "order").add(or);


    // apply restrictions.
    if (restrictions != null) {
        for (Criterion criterion : restrictions) {
            criteria.add(criterion);
        }
    }

    List list = criteria.list();
    if (list != null) {
        return list;
    }
    return Collections.emptyList();
}


生产的
在客户this_.ORDER_ID = order_.uid上的客户this_内部联接ORDER order_中选择.......其中(lcase(order_.NAME)如?)和(lcase(this_.name)如?或lcase(this_.state ) 喜欢 ?)

orderName只是在第二个或语句之前,但仍不在第二个或语句中……我很困惑。

最佳答案

好吧,您不必将限制添加到“条件”(创建“与”),而必须将其添加到quotedLikeRestrictions析取式中:

quotedLikeRestrictions.add(Restrictions.ilike("order.name", token, MatchMode.ANYWHERE));

关于java - 如何将Hibernate“createAlias”子级限制添加到父级实体限制,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22163158/

10-11 02:15