DetachedCriteria 关联查询
本帖最后由 qq245282209 于 2014-03-25 13:52:13 编辑
有如下两个实体类:
A.java
public class A{
private User user;
@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "user_id")
public User getUser() {
return user;
}
public void setUser(User user) {
this.user= user;
}
}
User.java
public class User{
private Integer id;
private String name;
getter and setter...
}
怎么用DetachedCriteria 查询A.java中user的name 为 "a"的数据??
dc.add(Restrictions.like("user.name", "a"));
这样查询要报错:
org.hibernate.QueryException: could not resolve property: user.name of: com.entity.health.Task
at
------解决方案--------------------.createAlias(“user”, “u”)
.add(Restrictions.like(“u.name”, “a%”))
------解决方案--------------------dc.createAlias("user", "user", DetachedCriteria.LEFT_JOIN);