-->
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.  [ 1 post ] 
Author Message
 Post subject: one-to-many relationship
PostPosted: Thu Sep 18, 2008 11:09 am 
Newbie

Joined: Thu Sep 18, 2008 10:46 am
Posts: 3
Hi all, I've a problem with a one-to-many relationship. Givin the following tables:

IDM_CODE_TABLE
-------------------------
INT ID (PK)
VARCHAR(64) NAME



IDM_RANGE
--------------------------------------------
INT ID (PK)
INT CODE_TABLE_ID (PK)
INT START_ID
INT END_ID



Here below are the mapping files:

<hibernate-mapping>
<class name="OjbIdmCodeTable" table="IDMCODE_TABLE">
<id name="ID" column="ID" type="java.lang.Long">
<generator class="increment"/>
</id>

<property name="name" column="NAME" type="java.lang.String"/>

<set name="ranges" inverse="true" cascade="all,delete-orphan">
<key column="CODE_TABLE_ID"/>
<one-to-many class="ObjIdmRange"/>
</set>
</class>
</hibernate-mapping>



<hibernate-mapping>
<class name="ObjIdmRange" table="IDM_RANGE">
<id name="ID" column="ID" type="java.lang.Long">
<generator class="increment"/>
</id>

<property name="codeTableID" column="CODE_TABLE_ID"
type="java.lang.Long"/>
<property name="startID" column="START_ID"
type="java.lang.Long"/>
<property name="endID" column="END_ID"
type="java.lang.Long"/>
</class>
</hibernate-mapping>



Here below is my code:

try {
transaction = session.beginTransaction();
Criteria c = session.createCriteria("ranges");
c.setFetchMode("ranges", FetchMode.JOIN);

List codeTables = session.createQuery(
"from ObjIdmCodeTable objIdmCodeTable").list();

for (Iterator iterator = codeTables.iterator(); iterator.hasNext(); ) {
ObjIdmCodeTable objIdmCodeTable =
(ObjIdmCodeTable) iterator.next();

System.out.println(" ID = " + objIdmCodeTable.getID());
System.out.println(" NAME = " + objIdmCodeTable.getName());
System.out.println();

java.util.Set ranges = objIdmCodeTable.getRanges();
for (Iterator rangesIterator = ranges.iterator();
rangesIterator.hasNext(); ) {
ObjIdmRange objIdmRange =
(ObjIdmRange) rangesIterator.next();

System.out.println(" START_ID = "
+ objIdmRange.getStartID());
System.out.println(" END_ID = "
+ objIdmRange.getEndID());
System.out.println();
}
}

transaction.commit();
} catch (HibernateException e) {
...
}



... and finally here are the Hibernate logs:

Hibernate: select objidmcode0_.ID as ID4_, objidmcode0_.NAME as NAME4_ from IDM_CODE_TABLE objidmcode0_
ID = 1
NAME = CODE_OVERRIDE

Hibernate: select ranges0_.CODE_TABLE_ID as CODE2_1_, ranges0_.ID as ID1_, ranges0_.ID as ID5_0_, ranges0_.CODE_TABLE_ID as CODE2_5_0_, ranges0_.START_ID as START3_5_0_, ranges0_.END_ID as END4_5_0_, ranges0_.OVERRIDE_START_ID as OVERRIDE5_5_0_, ranges0_.OVERRIDE_END_ID as OVERRIDE6_5_0_ from IDM_RANGE ranges0_ where ranges0_.CODE_TABLE_ID=?
START_ID = 1000
END_ID = 1999

START_ID = 5000
END_ID = 99999


The stuff above performs badly since Hibernate generates two distinct queries. I'm wondering if it is possible to let Hibernate generate a single query using a JOIN.

Any help is really appreciated!
Thanks,
Jeff


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

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.