简体   繁体   English

Hibernate:涉及一对多关系的示例查询

[英]Hibernate: Query By Example involving one-to-many relationship

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. 我最近开始使用Criteria API的示例组件查询,遇到一个奇怪的问题-尝试执行搜索时会抛出org.hibernate.QueryException。

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). 我有一个类A,作为其属性之一,它具有一组类B的实例(Set <B> listOfBs)。 This is mapped as a one-to-many relationship in A. 这被映射为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. 我希望在B的实例实例上设置条件查询,例如,指定所有B的属性值为“ somevalue”,然后应用该条件查找在其集合中所有具有B的A。 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): 我正在使用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>

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 org.hibernate.QueryException:无法解析属性:_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. 但是,在我的所有类中都没有声明过这样的属性名称-在我看来,这可能是Hibernate在幕后使用的使持久化看起来透明的任何工具的一部分。

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. 我正在使用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 异常表明无法在b中找到属性_com

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. 是属性的名称在A和B中相同,并且已设置相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM