filip-fku
filip-fku

Reputation: 3265

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.

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!

Upvotes: 1

Views: 2038

Answers (1)

Hemant Metalia
Hemant Metalia

Reputation: 30678

 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.

Upvotes: 1

Related Questions