8

我有三个实体,例如注册、用户和国家。基本上一个注册属于一个用户,一个用户属于一个国家。现在我正在尝试使用以下内容从注册中选择国家名称

        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: select usercountr1_.name as y0_ from voucherList this_

我注意到sql中没有加入。这就是查询失败的原因吗?我怎样才能解决这个问题 ?

4

1 回答 1

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();
于 2013-11-06T09:11:14.003 回答