-->
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.  [ 4 posts ] 
Author Message
 Post subject: Criteria query returns same item multiple times
PostPosted: Wed Jan 09, 2008 1:16 pm 
Regular
Regular

Joined: Mon Mar 20, 2006 10:49 pm
Posts: 59
Hi:

The following Criteria query returns a list which may contain the same Employee multiple times.

ICriteria query = SessionManager.CurrentSession.CreateCriteria(typeof(Employee));
query = query.Add(Expression.Like("LastName", m_strQueryString + "%"))
.Add(Expression.Like("FirstName", m_strQueryString + "%"));
query = query.AddOrder(Order.Asc("LastName"))
.AddOrder(Order.Asc("FirstName"));
query = query.CreateCriteria("EmployeeResourceUnits")
.CreateCriteria("Department")
.Add(Expression.Eq("DepartmentCode", "AAADM"));

System.Collections.IList queryResults = query.List();

Employee has a one-to-many set association of EmployeeResourceUnits as follows

<set name="EmployeeResourceUnits">
<key column="EmployeeId"/>
<one-to-many class="EmployeeResourceUnit"/>
</set>

EmployeeResourceUnit has a many-to-one association with Department

<class name="EmployeeResourceUnit" table="AddressBook.EmployeeResourceUnitPeopleImportView">
<cache usage="read-only"/>

<id type="Int32" name="Id" column="EmployeeResourceUnitId" unsaved-value="0">
<generator class="identity"/>
</id>
<many-to-one name="BusinessUnit" class="BusinessUnit" column="BusinessUnitId"/>
<many-to-one name="Department" class="Department" column="DepartmentId"/>
<property name="StartDate"/>
</class>

If an Employee has more than one EmployeeResourceUnit that matches the criterion DepartmentCode = 'AAADM', it appears once in the list for each EmployeeResourceUnit. That is, there is a list item for each row returned by the generated query.

Is this correct behavior, or should the items returned by query.List() be unique?

Hibernate version:
1.2.1

Code between sessionFactory.openSession() and session.close():
ICriteria query = SessionManager.CurrentSession.CreateCriteria(typeof(Employee));
query = query.Add(Expression.Like("LastName", m_strQueryString + "%"))
.Add(Expression.Like("FirstName", m_strQueryString + "%"));
query = query.AddOrder(Order.Asc("LastName"))
.AddOrder(Order.Asc("FirstName"));
query = query.CreateCriteria("EmployeeResourceUnits")
.CreateCriteria("Department")
.Add(Expression.Eq("DepartmentCode", "AAADM"));

System.Collections.IList queryResults = query.List();

Name and version of the database you are using:
SQL/Server 2005

_________________
Mike Abraham


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 09, 2008 1:19 pm 
Regular
Regular

Joined: Wed Jan 25, 2006 1:11 am
Posts: 118
Location: Copenhagen, Denmark
Take a look at:

http://forum.hibernate.org/viewtopic.php?t=982270


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 09, 2008 1:32 pm 
Regular
Regular

Joined: Mon Mar 20, 2006 10:49 pm
Posts: 59
Brilliant Many thanks - that was exactlt what I needed.

_________________
Mike Abraham


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 09, 2008 1:38 pm 
Regular
Regular

Joined: Wed Jan 25, 2006 1:11 am
Posts: 118
Location: Copenhagen, Denmark
Youre welcome, remember to read the complete thread so you don't get surprises when using SetMaxResults/SetFirstResult :-)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.