-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Query By Exampl question
PostPosted: Wed May 17, 2006 12:14 pm 
Newbie

Joined: Wed May 17, 2006 11:48 am
Posts: 5
I am using Hibernate 3.1.2 in a JBoss environment and PostgreeSQL database. I have two tables, say parent and child, mapped in a 1 to n association.

Parent mapping is:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.test">
<class dynamic-insert="true" dynamic-update="true" name="com.test.Parent" table="PARENT">
<id column="Parent_ID" name="Parent_ID">
<generator class="native"/></id>
<version access="property" column="Parent_Version" name="Parent_Version" unsaved-value="null"/>
<property column="name" name="name" not-null="false"/>
<bag cascade="save-update" inverse="true" lazy="true" name="Child">
<key><column name="Parent" not-null="false"/></key>
<one-to-many class="com.test.Child"/>
</bag>
</class>
</hibernate-mapping>

Child mapping is:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.test">
<class dynamic-insert="true" dynamic-update="true" name="com.test.Child" table="CHILD">
<id column="Child_ID" name="Child_ID"><generator class="native"/></id>
<version access="property" column="Child_Version" name=" Child _Version" unsaved-value="null"/>
<property column="name" name="name" not-null="false"/>
<many-to-one class="com.test.Parent" name="Parent">
<column name="Parent" not-null="false"/></many-to-one>
</class>
</hibernate-mapping>

Parent and Child classes have overridden equals and hashCode implementations that are working properly. I am trying to execute a Query by Example in Child to obtain all Child elements for a given Parent using the following code:

Parent p = findParentByName("Parent1");
System.out.println("Parent: " + p.getName());
Child c = new Child();
c.setParent(p);
Example example = Example.create(c);
Criteria criteria = sessionFactory.getCurrentSession()
.createCriteria("com.test.Child")
.add( example );
List children = criteria.list();
for (Object cx : children) {
System.out.println(" Child " + ((Child) dx).getName());
}

Instead of getting a list of Parent1’s children I get a list of all Child elements. If I change the code for a Child property that is not an association (as name) the Query By example works fine.

Thanks in advance for any help on that.

Marco


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 17, 2006 1:04 pm 
Newbie

Joined: Wed May 17, 2006 11:48 am
Posts: 5
I checked the Hibernate reference and found out that associations are ignored in queries by example.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.