我有以下的持久類:使用Hibernate的標準地圖中的過濾鍵和值
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在過濾代碼列表(例如其中,財產名爲「顏色」的碼的值是「綠色」
我用下面的底座標準:
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代替!
我也嘗試訪問這個字段而不創建別名,並且這種方式冬眠似乎訪問了正確的代碼類。我用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。
感謝
我注意到你正在使用@MapKeyClass的JPA 2.1特性。您是否一定會使用棄用的Hibernate Criteria API而不是JPA Criteria API? – Naros
@Naros代表micdcar回答 - 是的,不幸的是,這意味着將整個sessionfactory配置切換到entitymanager,這可以被探索,但不會在如此短的時間內發佈。因此,我們必須創建標準,它返回一個休眠條件。 – mmalmeida