我有一个表成员,它作为一个懒惰的加载实体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。
如何使用延迟加载关联实体的属性限制?
答案 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();