[hibernate-issues] [Hibernate-JIRA] Commented: (HHH-2794) Composite ID objects with set of objects with same composite ID using query loading only loads one object

Gail Badner (JIRA) noreply at atlassian.com
Fri Aug 17 13:57:13 EDT 2007


    [ http://opensource.atlassian.com/projects/hibernate/browse/HHH-2794?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_27881 ] 

Gail Badner commented on HHH-2794:
----------------------------------

Please attach a runnable test case (Java and mapping).

> Composite ID objects with set of objects with same composite ID using query loading only loads one object
> ---------------------------------------------------------------------------------------------------------
>
>                 Key: HHH-2794
>                 URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-2794
>             Project: Hibernate3
>          Issue Type: Bug
>          Components: core
>    Affects Versions: 3.2.4.sp1
>         Environment: Hibernate3 3.2.1, Database is DB2 v8.2.5
>            Reporter: Matthew Morrissette
>            Priority: Blocker
>
> Basically the gist is you have a parent data object with a composite identifier.  You then have a child object with the same composite identifier and a set mapping in the parent object to a set of child objects.  The set mapping uses a native-sql named query to load it's members (and relies upon that composite identifier for loading). When you query the parent object after retrieiving it from the session and iterate over the set, there is always zero or one members in the parent object no matter if that sql query returned multiple rows or not.  In the below example the printed result should show two child objects: one with value1=val1 and value2=val2 and one with value1=val3 and value2=val4.  However only the first child object is returned.
> See code examples below (sql for building db is included as well):
> TestParentObject.java:
> import java.io.Serializable;
> import java.util.Collection;
> import java.util.HashSet;
> import java.util.Iterator;
> public class TestParentObject implements Serializable
> {
> 	private long id1, id2;
> 	
> 	private Collection subobjects;
> 	/**
> 	 * @return the id1
> 	 */
> 	public long getId1()
> 	{
> 		return id1;
> 	}
> 	/**
> 	 * @param id1 the id1 to set
> 	 */
> 	public void setId1(long id1)
> 	{
> 		this.id1 = id1;
> 	}
> 	/**
> 	 * @return the id2
> 	 */
> 	public long getId2()
> 	{
> 		return id2;
> 	}
> 	/**
> 	 * @param id2 the id2 to set
> 	 */
> 	public void setId2(long id2)
> 	{
> 		this.id2 = id2;
> 	}
> 	/**
> 	 * @return the subobjects
> 	 */
> 	public Collection getSubobjects()
> 	{
> 		return subobjects;
> 	}
> 	/**
> 	 * @param subobjects the subobjects to set
> 	 */
> 	public void setSubobjects(Collection subobjects)
> 	{
> 		this.subobjects = subobjects;
> 	}
> 	
> 	public String toString()
> 	{
> 		String out = "Parent{id1=" + id1 + ",id2=" + id2 + ",subobjects=[";
> 		
> 		for (Iterator iter = subobjects.iterator();iter.hasNext();)
> 		{
> 			final TestSubObject obj = (TestSubObject)iter.next();
> 			out += "SubObject{id1=" + obj.getId1() + ",id2=" + obj.getId2() + ",value1=" + obj.getValue1() + ",value2=" + obj.getValue2() + "}";
> 		}
> 		out += "]}";
> 		return out;
> 	}
> }
> TestSubObject.java:
> import java.io.Serializable;
> public class TestSubObject implements Serializable
> {
> 	private TestParentObject parent;
> 	
> 	private String value1,value2;
> 	/**
> 	 * @return the id1
> 	 */
> 	public long getId1()
> 	{
> 		return parent.getId1();
> 	}
> 	/**
> 	 * @return the id2
> 	 */
> 	public long getId2()
> 	{
> 		return parent.getId2();
> 	}
> 	/**
> 	 * @return the value1
> 	 */
> 	public String getValue1()
> 	{
> 		return value1;
> 	}
> 	/**
> 	 * @param value1 the value1 to set
> 	 */
> 	public void setValue1(String value1)
> 	{
> 		this.value1 = value1;
> 	}
> 	/**
> 	 * @return the value2
> 	 */
> 	public String getValue2()
> 	{
> 		return value2;
> 	}
> 	/**
> 	 * @param value2 the value2 to set
> 	 */
> 	public void setValue2(String value2)
> 	{
> 		this.value2 = value2;
> 	}
> 	/**
> 	 * @return the parent
> 	 */
> 	public TestParentObject getParent()
> 	{
> 		return parent;
> 	}
> 	/**
> 	 * @param parent the parent to set
> 	 */
> 	public void setParent(TestParentObject parent)
> 	{
> 		this.parent = parent;
> 	}
> }
> TestParentObject.hbm.xml:
> <?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.harrahs.enterprise.ggwu.dataobjects">
> 	<class name="TestParentObject" table="PARENT"
> 		<composite-id>
> 			<key-property name="id1" type="long" />
> 			<key-property name="id2" type="long" />
> 		</composite-id>
> 		<set name="subobjects" inverse="true">
> 			<key>
> 				<column name="id1" />
> 				<column name="id2" />
> 			</key>
> 			<one-to-many class="TestSubObject" />
> 			<loader query-ref="subObj" />
> 		</set>
> 	</class>
> 	<class name="TestSubObject" table="SUBOBJECT">
> 		<composite-id>
> 			<key-many-to-one name="parent" class="TestParentObject">
> 				<column name="id1"/>
> 				<column name="id2"/>
> 			</key-many-to-one>
> 		</composite-id>
> 		<property name="value1"/>
> 		<property name="value2"/>
> 	</class>
> 	<sql-query name="subObj">
> 		<load-collection alias="sub" role="TestParentObject.subobjects" />
> 		SELECT {sub.*} FROM SUBOBJECT AS SUB WHERE ID1 = ? AND ID2 = ? WITH UR
> 	</sql-query>
> </hibernate-mapping>
> TestObject.java:
> import junit.framework.TestCase;
> public class TestObject extends TestCase
> {
> 	public void testParentObj()
> 	{
> 		final Session session = HibernateUtil.openSession();
> 		
> 		session.beginTransaction();
> 		
> 		final TestParentObject obj = (TestParentObject)session.createCriteria(TestParentObject.class).add(Restrictions.eq("id1",new Long(1))).add(Restrictions.eq("id2",new Long(2))).uniqueResult();
> 		
> 		System.out.println(obj);
> 		
> 		session.getTransaction().commit();
> 		
> 		session.flush();
> 		
> 		HibernateUtil.closeSession();
> 	}
> }
> GenerateDB.sql:
> CREATE TABLE PARENT (
> 	ID1 BIGINT,
> 	ID2 BIGINT) IN USERSPACE1;
> CREATE TABLE SUBOBJECT (
> 	ID1 BIGINT,
> 	ID2 BIGINT,
> 	VALUE1 VARCHAR(255),
> 	VALUE2 VARCHAR(255)) IN USERSPACE1;
> INSERT INTO VALUES (1,2);
> INSERT INTO PARENT VALUES (2,2);
> INSERT INTO SUBOBJECT VALUES (1,2,'val1','val2');
> INSERT INTO SUBOBJECT VALUES (1,2,'val3','val4');
> INSERT INTO SUBOBJECT VALUES (2,2,'val5','val6');

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://opensource.atlassian.com/projects/hibernate/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        



More information about the hibernate-issues mailing list