我有三个实体,例如注册,用户和国家。基本上,注册属于用户,用户属于国家。现在我尝试从以下
注册中选择国家/地区名称 Criteria criteria = getSession().createCriteria(Registration.class);
ProjectionList projectionList = Projections.projectionList();
criteria.createAlias("user.country", "usercountry");
projectionList.add(Projections.property("usercountry.name"),"usercountry.name");
criteria.setProjection(projectionList);
criteria.list();
这失败并给我:
ERROR: org.hibernate.engine.jdbc.spi.SqlExceptionHelper - Unknown column 'usercountr1_.name' in 'field list'
org.hibernate.exception.SQLGrammarException: Unknown column 'usercountr1_.name' in 'field list'
生成的Hibernate查询:
Hibernate: select usercountr1_.name as y0_ from voucherList this_
我注意到sql中没有连接。这是查询失败的原因吗?我该如何解决这个问题?
答案 0 :(得分:13)
尝试类似
的内容Criteria criteria = getSession().createCriteria(Registration.class);
ProjectionList projectionList = Projections.projectionList();
criteria.createAlias("user", "u"); // here i changed
criteria.createAlias("u.country", "usercountry"); // here i have changed
projectionList.add(Projections.property("usercountry.name"),"usercountry.name");
criteria.setProjection(projectionList);
criteria.list();