Hibernate:涉及一对多关系的示例查询
我最近开始使用 Criteria API 的示例组件进行查询,并遇到了一个奇怪的问题 - 尝试执行搜索时抛出 org.hibernate.QueryException。
我的场景如下:
我有一个类 A,它的一个属性具有一组 B 类的实例(SetlistOfBs)。这在 A 中映射为一对多关系。
我希望在 B 的示例实例上设置条件查询,例如指定属性值为“somevalue”的所有 B,然后应用该条件来查找所有 A 的集合中都有这样的 B。这是我正在使用(或希望)的代码:
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();
我正在使用 XML 映射,A 正在将其与 B 的关系映射如下(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>
我意识到这可能不是正确的方法 - 更好欢迎提出建议。无论如何,问题是我遇到了异常:
org.hibernate.QueryException:无法解析属性:_com:B
我已经搜索并意识到异常告诉我什么。然而,在我的任何类中都没有声明这样的属性名称 - 在我看来,这可能是 Hibernate 在幕后使用的任何工具的一部分,以使持久性看起来透明。
我很好奇这是否是一个已知问题,人们是否使用过解决方法,或者可能是新版本中的解决方案?我正在使用 Hibernate 3.6.6。
任何建议/经验将不胜感激。
谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
异常表明它无法在 b 中找到属性 _com,
这意味着您的以下代码是错误的。
A 和 B 中的属性名称相同且与您设置的相同。
exception tells that it couldnt find the property _com in b
it means that in following code of yours is erroneous.
is the name of property is same in A and B and same which you have set.