gpt4 book ai didi

java - 为什么 Hibernate HQL 可以工作,而 Criteria 却不能?

转载 作者:太空宇宙 更新时间:2023-11-04 08:28:45 26 4
gpt4 key购买 nike

我有一个简单的模型,它只是一对多关系链:国家 -< 城市 -< 街道这些表被映射为实体并作为 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 的顶部 block 按预期工作,但底部 block 崩溃了:

输出:

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(限制)却不能。

我是否遗漏了一些明显的东西?

最佳答案

因为您在 hql 查询中使用了错误的语法。几个错误:

  • Street.class 而不是“Street”
  • 缺少别名
  • 直接使用国家/地区而不是country.id

试试这个:

Criteria criteria = session.createCriteria(Street.class)
.createAlias("city", "ci")
.add(Restrictions.eq("ci.country", country))

请参阅hibernate reference了解更多详情。

关于java - 为什么 Hibernate HQL 可以工作,而 Criteria 却不能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7965572/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com