我有一个简单的模型,它只是一对多关系链:Country - <城市 - <街 这些表被映射为实体,并作为Map返回。
以下测试方法产生奇怪的结果:
public static void main(String[] args) {
Session session = HibernateSessionFactory.getSession();
List<Map<String, Object>> results = null;
//Query using HQL and print results
System.out.println("FROM HQL =====================");
String hql = "from Street where City.Country.countryid = 1";
Query query = session.createQuery(hql);
results = query.list();
for(Map<String, Object> row : results) {
System.out.println(row);
}
//Query using Criteria and print results
System.out.println("FROM CRITERIA ================");
Criteria criteria = session.createCriteria("Street");
criteria.add(Restrictions.eq("City.Country.countryid", 1));
results = criteria.list();
for(Map<String, Object> row : results) {
System.out.println(row);
}
}
使用HQL的顶部块按预期工作,但底部块无效:
输出:
FROM HQL =====================
{streetname=Mayfair, City=org.hibernate.proxy.map.MapProxy@2b12e7f7, $type$=Street, streetid=1}
{streetname=Park Lane, City=org.hibernate.proxy.map.MapProxy@2b12e7f7, $type$=Street, streetid=2}
{streetname=Bond Street, City=org.hibernate.proxy.map.MapProxy@663b1f38, $type$=Street, streetid=3}
{streetname=Old Kent Road, City=org.hibernate.proxy.map.MapProxy@663b1f38, $type$=Street, streetid=4}
FROM CRITERIA ================
Exception in thread "main" org.hibernate.QueryException: could not resolve property: City.Country.countryid of: Street
at org.hibernate.persister.entity.AbstractPropertyMapping.propertyException(AbstractPropertyMapping.java:67)
at org.hibernate.persister.entity.AbstractPropertyMapping.toColumns(AbstractPropertyMapping.java:82)
at org.hibernate.persister.entity.BasicEntityPropertyMapping.toColumns(BasicEntityPropertyMapping.java:54)
at org.hibernate.persister.entity.AbstractEntityPersister.toColumns(AbstractEntityPersister.java:1367)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getColumns(CriteriaQueryTranslator.java:457)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getColumnsUsingProjection(CriteriaQueryTranslator.java:417)
at org.hibernate.criterion.SimpleExpression.toSqlString(SimpleExpression.java:68)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getWhereCondition(CriteriaQueryTranslator.java:357)
at org.hibernate.loader.criteria.CriteriaJoinWalker.<init>(CriteriaJoinWalker.java:113)
at org.hibernate.loader.criteria.CriteriaJoinWalker.<init>(CriteriaJoinWalker.java:82)
at org.hibernate.loader.criteria.CriteriaLoader.<init>(CriteriaLoader.java:91)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1578)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:306)
at com.bar.foo(Main.java:33)
我看不出为什么HQL可以解析City.Country.countryid但是Criteria(限制)不能解析。
我错过了一些明显的东西吗?
答案 0 :(得分:1)
因为您对hql查询使用了错误的语法。 几个错误:
试试这个:
Criteria criteria = session.createCriteria(Street.class)
.createAlias("city", "ci")
.add(Restrictions.eq("ci.country", country))
有关详细信息,请参阅hibernate reference。