-->
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.  [ 5 posts ] 
Author Message
 Post subject: Using the crit. API to find rows w/o a corresponding 1:1 rel
PostPosted: Sat Sep 24, 2005 4:26 am 
Newbie

Joined: Wed Jan 05, 2005 2:28 pm
Posts: 14
Location: Germany, Munich
Hello,

I'm trying to use the criteria API to find rows without a corresponding 1:1 relation. The native SQL looks like this:

Code:
select      tableA.foo
from        tableA
left join   tableB
on          tableA.id = tableB.id
where       tableB.id IS NULL


I tried it with:

Code:
Criteria crit = session.createCriteria(TableA.class);
crit.add(Restrictions.isNull("propertyForTableBObject"));


but this results in something like this:

Code:
select      tableA.foo
from        tableA
left join   tableB
on          tableA.id = tableB.id
where       tableA.id IS NULL



Any hints?


Oliver


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 25, 2005 1:25 pm 
Senior
Senior

Joined: Tue Aug 23, 2005 8:52 am
Posts: 181
I created a many-to-one between Product and Category and creating a Criteria query like

Criteria crit = session.createCriteria(test.Category.class).createCriteria("products");
crit.add(Restrictions.isNull("category"));

and it did create a query like
Hibernate: select this_.CATEGORY_ID as CATEGORY1_1_, this_.CATEGORY_NAME as CATEGORY2_0_1_, product1_.PRODUCT_ID as PRODUCT1_0_, product1_.PRODUCT_NAME as PRODUCT2_1_0_, product1_.CATEGORY_ID as CATEGORY3_1_0_
from CATEGORY this_ inner join PRODUCT product1_
on this_.CATEGORY_ID=product1_.CATEGORY_ID
where product1_.CATEGORY_ID is null.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 25, 2005 2:15 pm 
Newbie

Joined: Wed Jan 05, 2005 2:28 pm
Posts: 14
Location: Germany, Munich
rajasaur wrote:
I created a many-to-one between Product and Category and creating a Criteria query like

Criteria crit = session.createCriteria(test.Category.class).createCriteria("products");
crit.add(Restrictions.isNull("category"));

and it did create a query like
Hibernate: select this_.CATEGORY_ID as CATEGORY1_1_, this_.CATEGORY_NAME as CATEGORY2_0_1_, product1_.PRODUCT_ID as PRODUCT1_0_, product1_.PRODUCT_NAME as PRODUCT2_1_0_, product1_.CATEGORY_ID as CATEGORY3_1_0_
from CATEGORY this_ inner join PRODUCT product1_
on this_.CATEGORY_ID=product1_.CATEGORY_ID
where product1_.CATEGORY_ID is null.


This seems to me really senseless. Your query will never return any rows, because if the where clause is true, the join condition cannot be true - due to the inner join no result is ever possible.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 26, 2005 12:13 am 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
You want

Code:
where       tableB.id IS NULL

but you are getting

Code:
where       tableA.id IS NULL

because you are coding

Code:
Criteria crit = session.createCriteria(TableA.class);


how can you get criteria for B if A is what your passing to createCriteria ?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 26, 2005 2:04 am 
Newbie

Joined: Wed Jan 05, 2005 2:28 pm
Posts: 14
Location: Germany, Munich
dennisbyrne wrote:
how can you get criteria for B if A is what your passing to createCriteria ?


You're right, but if I pass TableB to createCriteria (in addition to TableA - I need criteria there, too) then Hibernate will form an inner join instead of a left join. Due to the inner join no result is possible.


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