//部门bean
public class Dept{
private String id;
private String name;
@OneToMany(mappedBy="dept")
private Set<User> users = new HashSet<User>();
}    //人员bean
       public class User{
private String id;
@ManyToOne
private Dept dept;
}       public static void main(String[] args) {
ApplicationContext applicationContext = new            ClassPathXmlApplicationContext(
"applicationContext.xml");
 

DetachedCriteria criteria = DetachedCriteria.forClass(SystemUser.class);
// criteria.createAlias("dept", "d");
                // criteria.add(Restrictions.eq("d.name", "12"));
criteria.add(Restrictions.eq("dept.name", "12"));
criteria.getExecutableCriteria(DBControl.getHibernateTemplate()
.getSessionFactory().openSession());
}请问我在使用hibernate, 比如一个user 实体里包含了一个dept的对象 我使用hql的时候 直接 "from User where dept.name like '%xx%'" 这个时候是可以查的.
但是在使用qbc的时候 我加入查询条件 criteria.add(Restrictions.eq("dept.name", "12")) 的时候
却说该属性不存在 ,可是我加入条件 criteria.add(Restrictions.eq("dept.id", "12"))的时候确不报错也是可以查的.也就是说使用qbc的时候,查关联对象,除了查询关联对象的主键可以查询到以外,查询其他的任何字段都得取了别名之后再来查询
 criteria.createAlias("dept", "d");
criteria.add(Restrictions.eq("d.name", "12"));
这样就可以关联dept的其他属性.请问这是为什么呢?
hibernate 为什么要如此设计呢,直接和hql一样直接对象名.属性查出来不就可以了嘛?
求解释哈。
解决了人家鲍照.
hibernatejavaqbchql