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: Criteria for selecting instances of derived subclass only
PostPosted: Tue Aug 28, 2007 8:13 am 
Newbie

Joined: Fri Sep 29, 2006 6:52 am
Posts: 17
Having the following classes:

Code:
class SaleItem {}
class ProductSaleItem : public SaleItem {}
class SubtotalSaleItem : public SaleItem {}
class ItemCancellation : public SaleItem
{
    SaleItem CancelledItem { get ... }
}


when using a joint subclass mapping, I want to do this basic SQL query:

Code:
select * from ITEMCANCELLATION i inner join PRODUCTSALEITEM p on i.CANCELLEDITEM = p.ID


So I want all ItemCancellations, that refer to a ProductSaleItem.

In HQL this can be done like this :

Code:
select from ItemCancellation i, ProductSaleItem p where i.CancelledItem = p.Id


But how can this be done with a Criteria query (without using sub queries)?

Tobias


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 30, 2007 4:40 pm 
Expert
Expert

Joined: Fri May 13, 2005 11:13 am
Posts: 292
Location: Rochester, NY
You're right, there doesn't seem to be a simple join method for this using Criteria, but is there a reason subqueries are not an option?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 31, 2007 5:20 am 
Newbie

Joined: Fri Sep 29, 2006 6:52 am
Posts: 17
No. I was just curious, if there is another way. At the moment I'm doing it this way:

Code:
DetachedCriteria itemCancellations = DetachedCriteria.For<ItemCancellation>()
    .SetProjection(Projections.Property("Id"))
    .Add(Expression.EqProperty("CancelledItem", "item.Id"));

ICriteria criteria = session.CreateCriteria(typeof(ProductSaleItem), "item")
    .Add(Subqueries.Exists(itemCancellations))


But the resulting SQL might not be as efficient as the simple one, I mentioned above.

HQL seems to be more handy in this case, but I need an AliasToEntityMap transformer, which can't be used with HQL queries. (The actual query is much more complicated and includes a lot of aggregate projections and grouping)


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.