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.  [ 3 posts ] 
Author Message
 Post subject: ICriteria VS HQL - ICriteria returns multiple results
PostPosted: Tue Aug 15, 2006 11:21 am 
Newbie

Joined: Tue Aug 15, 2006 11:01 am
Posts: 10
Hibernate version: 1.2.0.Alpha1

Name and version of the database you are using: SQLServer 2000
Mapping
Code:
<class name="CDS.HEPS.DocumentLibrary.Document, CDS.HEPS.DocumentLibrary" table="`Library Item`" >
    <id name="Id" column="ItemId" unsaved-value="0" >
      <generator class="identity" />
    </id>
    <property name="Title" />
    <property name="CommentsRecipient" />
    <property name="Summary" />
    <property name="PublishedDate" />
    <many-to-one name="State" column="StateId" />
    <set name="Categories" table="LibraryItemCategory" fetch="join" >
      <key column="ItemId"/>
      <many-to-many column="CategoryId" class="CDS.HEPS.DocumentLibrary.Category, CDS.HEPS.DocumentLibrary" />
    </set>
    <component name="ReviewPanel">
      <set name="Users" table="`Library Item Reviewer`" fetch="join"  >
        <key column="ItemId"/>
        <many-to-many column="Id" class="CDS.HEPS.DocumentLibrary.ExtranetUser, CDS.HEPS.DocumentLibrary" />
      </set>
      <set name="Groups" table="`LibraryItemReviewGroup`" fetch="join" >
        <key column="ItemId"/>
        <many-to-many column="ExtranetGroupId" class="CDS.HEPS.DocumentLibrary.ExtranetGroup, CDS.HEPS.DocumentLibrary"  />
      </set>
    </component>
    <component name="ReviewSchedule">
      <set name="Items" >
        <key column="ItemId" />
        <one-to-many class="CDS.HEPS.DocumentLibrary.ReviewScheduleItem, CDS.HEPS.DocumentLibrary" />
      </set>
     
    </component>
    <set name="Files" fetch="join" lazy="false" >
      <key column="ItemId"/>
      <one-to-many class="CDS.HEPS.DocumentLibrary.File, CDS.HEPS.DocumentLibrary" />
    </set>
  </class>


I get duplicate rows when I run:
Code:
session.CreateCriteria(typeof(Document)).AddOrder(Order.Asc("Title")).List()

However, using HQL (as follows) there are no duplicate rows.
Code:
session.CreateQuery ( "from Document Document order by Document.Title" ).List ( ) ;


for some reason, the first case runs the following SQL:
Code:
SELECT this_.ItemId as ItemId1_, this_.StateId as StateId0_1_, this_.Title as Title0_1_, this_.PublishedDate as Publishe5_0_1_,
this_.Summary as Summary0_1_, this_.CommentsRecipient as Comments3_0_1_, categories2_.ItemId as ItemId__3_, categories2_.CategoryId
as CategoryId3_, users3_.ItemId as ItemId__4_, users3_.Id as Id4_, groups4_.ItemId as ItemId__5_, groups4_.ExtranetGroupId as
Extranet2_5_, files5_.ItemId as ItemId__6_, files5_.FileId as FileId6_, files5_.FileId as FileId0_, files5_.Title as Title6_0_,
files5_.Author as Author6_0_, files5_.FileRef as FileRef6_0_, files5_.ContentType as ContentT6_6_0_, files5_.FileSize as FileSize6_0_
FROM [Library Item] this_ left outer join LibraryItemCategory categories2_ on this_.ItemId=categories2_.ItemId left outer join
[Library Item Reviewer] users3_ on this_.ItemId=users3_.ItemId left outer join [LibraryItemReviewGroup] groups4_ on
this_.ItemId=groups4_.ItemId left outer join [File Item] files5_ on this_.ItemId=files5_.ItemId ORDER BY this_.Title asc


and the second case runs the much simpler:
Code:
select document0_.ItemId as ItemId, document0_.StateId as StateId0_, document0_.Title as Title0_, document0_.PublishedDate as
Publishe5_0_, document0_.Summary as Summary0_, document0_.CommentsRecipient as Comments3_0_ from [Library Item] document0_ order by 
document0_.Title


the code with the entities and the mappings is a http://www.box.net/public/sr58o9pvn7.

Thanks for your help


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 16, 2006 8:51 am 
Regular
Regular

Joined: Wed May 05, 2004 3:41 pm
Posts: 118
Location: New Jersey,USA
In the HQL case you are selecting only the entity and not int's relating i.e. dependent objects. If you were do do something like this:

Code:
select Doc,Cat
from Document Doc
left outer join Doc.Categories cat
order by Doc.Title


You'll see part of the SQL generated will be similar to the Query generated in the Criteria method.

Since your HQL does not metion any relationships to be fetched, it did not fetch them.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 16, 2006 9:33 am 
Newbie

Joined: Tue Aug 15, 2006 11:01 am
Posts: 10
thanks for your reply anandn.

I suppose what I want to know is why the Criteria method is returning the duplicates...


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