-->
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: one-to-many weirdness - have never seen this...
PostPosted: Mon Mar 06, 2006 4:59 pm 
Regular
Regular

Joined: Mon Nov 14, 2005 7:33 pm
Posts: 73
I've got a simple one-to-many relationship mapped between an Employee object and a LeaveRequest object (one object per-table) and it's generating some SQL I would have not expected to see...since this is very typical relationship.

Employee mapping:

Code:
<hibernate-mapping>
   <class name="com.app.Employee" table="ed_employee">
     <id name="Id" column="employee_id" type="int" unsaved-value="0">
       <generator class="identity"/>
    </id>
     <property name="DirectoryId" column="directory_id"/>
     <property name="FirstName" column="first_name"/>
     <property name="MidInitial" column="mid_initial"/>
     <property name="LastName" column="last_name"/>
      <property name="Extension" column="extension"/>
     <property name="Status" column="status"/>
      <property name="Comment" column="comment"/>
     <bag
        name="LeaveRequests"
        table="ed_leave_request"
        lazy="true"
         inverse="true"
        fetch="select"
        cascade="none">
        <key column="employee_id" />
        <one-to-many class="com.app.LeaveRequest" />
     </bag>
   </class>
</hibernate-mapping>


LeaveRequest mapping:

Code:
<hibernate-mapping>
   <class name="com.app.LeaveRequest" table="ed_leave_request">
     <id name="Id" column="rfl_id" type="int" unsaved-value="0">
      <generator class="identity"/>
    </id>
     <property name="EmployeeId" column="employee_id"/>
     <property name="LeaveDate" column="leave_date"/>
     <property name="ReturnDate" column="return_date"/>
     <property name="DaysTotal" column="days_total"/>
     <property name="Reason" column="reason"/>
     <property name="ReasonOther" column="reason_other"/>
     <property name="RequestDate" column="request_date"/>
     <property name="Approved" column="is_approved"/>
      <many-to-one
        name="Employee"
         column="employee_id"
         lazy="false"
        insert="false"
         update="false"
         class="com.app.Employee" />
   </class>
</hibernate-mapping>


SQL output (fields abbreviated w/ '*' for readability):

Code:
select * from ed_employee this_ order by this_.first_name asc, this_.last_name asc
select * from ed_leave_request leavereque0_ where leavereque0_.employee_id in (?, ?, ?, ?, ?, ?, ?, ?)
select * from ed_leave_request leavereque0_ where leavereque0_.employee_id in (?, ?, ?, ?, ?, ?, ?, ?)
select * from ed_leave_request leavereque0_ where leavereque0_.employee_id in (?, ?, ?, ?, ?, ?, ?, ?)
select * from ed_leave_request leavereque0_ where leavereque0_.employee_id in (?, ?, ?, ?, ?, ?, ?, ?)
select * from ed_leave_request leavereque0_ where leavereque0_.employee_id in (?, ?, ?, ?, ?, ?, ?, ?)
select * from ed_leave_request leavereque0_ where leavereque0_.employee_id in (?, ?, ?, ?, ?, ?)


I would have expected to see a single "select x,y,z from requests where employee_id = ?" type of query...not a whole slew of them testing against a sub-query. Everything *works* but obviously, I'd like to see less queries being generated.

I would believe it's the n+1 selects problem but this is test data and only one of the employee records as leave request records...and that employee only has 3 records...so I'm probably just not seeing a goof in my mapping files somewhere.

I've tried tweaking it by removing the inverse relationship...but the problem returns. I can't use a join w/o making the employee show up 3 times in the master list...so I'd rather use select.

I'm using the official MSSQL 2000 JDBC driver...and this type of mapping is working elsewhere in my app w/o this type of strangeness.

Any ideas?

Hibernate version: 3.1.2

Name and version of the database you are using: MSSQL 2000


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 06, 2006 10:34 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
What's the query that's generating this? You've used fetch="select", so you're going to get one LeaveRequest select per employee. If your query loads many employees, you'll get many LeaveRequest selects.

I'm curious about this statement:
tsar bomba wrote:
I can't use a join w/o making the employee show up 3 times in the master list...so I'd rather use select.

That's an odd one. The only thing I can think of that produces this sort of effect is when you're doing Criteria queries with the wrong result transformer. Is that what you're doing? If you're using Criteria, you should almost always setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY), unless you have a good reason to use one of the other transformers. You might find that switching back to fetch="join" will work for you, if you do this.


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.