【发布时间】:2016-12-26 21:46:27
【问题描述】:
我有以下持久类:
public class Code {
@ElementCollection(targetClass = CodeValue.class)
@MapKeyClass(CodeProperty.class)
@JoinTable(name="code_properties")
@CreateIfNull( value = false )
private Map<CodeProperty,CodeValue> propertiesMap =
new HashMap<CodeProperty, CodeValue>();
...
}
public class CodeProperty {
private String name;
...
}
public class CodeValue {
private String value;
...
}
我正在尝试获取由 propertiesMap 中的一些属性过滤的代码列表(例如,名为“color”的属性的值为“green”的代码。
我使用以下基本标准:
Criteria criteria = currentSession()
.createCriteria(Code.class, "code")
.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
当我尝试执行收集过滤器时(建议 here):
criteria.createAlias("code.properties", "p");
criteria.add(Restrictions.eq("p.foo", "test1"));
criteria.setFetchMode("code.properties", FetchMode.JOIN);
criteria.list();
我收到以下错误:
org.hibernate.QueryException: could not resolve property: foo of: com.example.CodeValue
这意味着,我真的不明白为什么,hibernate 认为 code.properties 是 CodeValue 而不是 map !!!
我也尝试在不创建别名的情况下访问该字段,这样 hibernate 似乎可以访问正确的 Code 类。我使用了 properties.indeces(建议 here):
criteria.add(Restrictions.eq("properties.indeces", "foo"));
criteria.list();
但是这样我得到以下错误:
could not resolve property: properties.indeces of: com.example.Code
有人可以帮助我了解问题所在吗?找到绿色代码的正确条件查询是什么?
您可以结帐the Github project that demonstrates this problem。
谢谢
【问题讨论】:
-
我注意到您将 JPA 2.1 功能与 @MapKeyClass 一起使用。您是否一定要使用已弃用的 Hibernate Criteria API 而不是 JPA Criteria API?
-
@Naros 代表 micdcar 回答 - 是的,不幸的是,这意味着将整个 sessionfactory 配置切换到 entitymanager,可以探索但不能在这么短的时间内进行。所以我们必须创建 Criteria,它返回休眠条件。
标签: java hibernate hibernate-criteria