Hibernate:包含一对多关系的示例查询 [英] Hibernate: Query By Example involving one-to-many relationship
问题描述
我最近开始使用Criteria API的示例组件查询,并遇到了一个奇怪的问题 - 尝试执行搜索时抛出org.hibernate.QueryException。
我的场景如下所示:
我有一个类A,它的一个属性有一组类B的实例(Set< ; B> listOfBs)。这被映射为A中的一对多关系。
我希望在B的示例实例上设置标准查询,例如指定所有B一个属性值为somevalue,然后应用该标准来查找所有在其集合中具有这样的B的A。这是我正在使用(或希望)的代码:
条件aCrit = session.createCriteria(A.class);
a aExampleInstance = new A();
示例aExampleCriteria = Example.create(aExampleInstance);
标准bCrit = atCrit.createCriteria(listOfBs);
B bExampleInstance = new B();
bExampleInstance .setProperty(somevalue);
bCrit.add(Example.create(bExampleInstance));
列表< A> results = aCrit.add(aExampleCriteria).list();
我正在使用XML映射,而A映射它与B的关系如下(A.hbm。 xml):
< set name =listOfBstable =Binverse =falsecascade =all懒惰= 真 >
< key column =A_IDnot-null =true/>
<一对多课程=B/>
< / set>
我意识到这可能不是正确的方法 - 任何更好的建议都是值得欢迎的。无论如何,麻烦的是我得到一个异常:
org.hibernate.QueryException:无法解析属性:_com of:B
我已经搜索并意识到异常告诉我的是什么。然而,在我的任何一个类中都没有声明这样的属性名 - 在我看来,这可能是Hibernate在引导下使用的任何工具的一部分,以使持久性看起来透明。
我很好奇,如果这是一个已知问题,是否有人们使用的解决方法,或者可能是更新版本的解决方案?我使用Hibernate 3.6.6。
任何建议/经验将不胜感激。
谢谢! p>
org.hibernate.QueryException:无法解析属性:_com:B
异常说明它找不到b中的属性_com b
$ b <这意味着在你的以下代码中是错误的。
B bExampleInstance = new B();
bExampleInstance .setProperty(somevalue);
是A和B中相同的属性名称,与您设置的相同。
I've recently started playing with the query by example component of the Criteria API and have run into a strange issue - an org.hibernate.QueryException is thrown when trying to perform a search.
My scenarios is as follows:
I have a class A, which as one of its properties has a set of instances of class B (Set< B> listOfBs). This is mapped as a one-to-many relationship in A.
I was hoping to set a criteria query on an example instance of B, for example specifying all B's with a property value of "somevalue", and then apply that criteria to find all A's that have such a B in their set. This is the code I am using (or hoping to):
Criteria aCrit = session.createCriteria(A.class);
A aExampleInstance = new A();
Example aExampleCriteria = Example.create(aExampleInstance );
Criteria bCrit = atCrit.createCriteria("listOfBs");
B bExampleInstance = new B();
bExampleInstance .setProperty("somevalue");
bCrit.add(Example.create(bExampleInstance ));
List<A> results = aCrit.add(aExampleCriteria).list();
I am using XML mapping, and A is mapping it's relation to B as follows (A.hbm.xml):
<set name="listOfBs" table="B" inverse="false" cascade="all" lazy="true">
<key column="A_ID" not-null="true"/>
<one-to-many class="B"/>
</set>
I realize that this might not be the right approach - any better suggestions are welcome. In any case, the trouble is that I get an exception:
org.hibernate.QueryException: could not resolve property: _com of: B
I have searched and realize what the exception is telling me. However there is no such property name declared in any of my classes - it would seem to me that this might be part of whatever instrumentation Hibernate uses under the hood to make persistence seem transparent.
I'm curious if this is a known issue, whether there is a workaround people have used, or perhaps a resolution in a newer version? I am using Hibernate 3.6.6.
Any advice/experiences would be appreciated.
Thanks!
org.hibernate.QueryException: could not resolve property: _com of: B
exception tells that it couldnt find the property _com in b
it means that in following code of yours is erroneous.
B bExampleInstance = new B();
bExampleInstance .setProperty("somevalue");
is the name of property is same in A and B and same which you have set.
这篇关于Hibernate:包含一对多关系的示例查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!