如何根据延迟加载实体的属性进行过滤

时间:2017-01-24 06:14:58

标签: sql hibernate hql

我有一个表成员,它作为一个懒惰的加载实体User。

Member   User
------   ----
id       id
User     name 

DetachedCriteria dc = DetachedCriteria.forClass(Member.class, "m");
dc.setFetchMode("user",FetchMode.JOIN);
dc.add(Restrictions.like("user.name", "alpha", MatchMode.ANYWHERE));
Criteria criteria = dc.getExecutableCriteria(session.getSession());
List<Membership> list = criteria.list();

抛出hibernate异常无法解析成员表中的属性user.name。

如何使用延迟加载关联实体的属性限制?

1 个答案:

答案 0 :(得分:1)

您需要创建别名才能访问用户的属性。为此,请更改为以下内容:

DetachedCriteria dc = DetachedCriteria.forClass(Member.class, "m");
dc.createAlias("user", "u");
dc.add(Restrictions.like("u.name", "alpha", MatchMode.ANYWHERE));
Criteria criteria = dc.getExecutableCriteria(session.getSession());
List<Membership> list = criteria.list();