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
|