-->
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.  [ 7 posts ] 
Author Message
 Post subject: Hibernate Query for missing one to one association
PostPosted: Sat Jan 14, 2012 7:16 am 
Newbie

Joined: Sat Jan 14, 2012 7:12 am
Posts: 7
Hello folks,

I have a optional one-to-one mapping

ClassA <-> ClassB

Now, I want all the instances of ClassA which do not have an associated instance of ClassB

How do I do that?

I have tried various options, but nothing seems to work.

thanks


SD


Top
 Profile  
 
 Post subject: Re: Hibernate Query for missing one to one association
PostPosted: Sat Jan 14, 2012 7:37 am 
Newbie

Joined: Sat Jan 14, 2012 7:12 am
Posts: 7
It is the exact opposite of

getSession().createCriteria(ClassA.class).createCriteria("classB").list();

Where classB is the name of the association (role) in this Association.

I am a bit stumped here!


Top
 Profile  
 
 Post subject: Re: Hibernate Query for missing one to one association
PostPosted: Mon Jan 16, 2012 9:06 am 
Newbie

Joined: Sat Jan 14, 2012 7:12 am
Posts: 7
I have one working solution.

I am using the following right now ->


Query query = getSession().createQuery("from ClassA as ca where not exists (from ClassB as cb where cb.classA=ca)");
query.list();

Is there a more efficient way to do the same?

Also, is there a way to do the same using Criteria API?


Top
 Profile  
 
 Post subject: Re: Hibernate Query for missing one to one association
PostPosted: Mon Jan 16, 2012 9:06 am 
Newbie

Joined: Sat Jan 14, 2012 7:12 am
Posts: 7
I have one working solution.

I am using the following right now ->


Query query = getSession().createQuery("from ClassA as ca where not exists (from ClassB as cb where cb.classA=ca)");
query.list();

Is there a more efficient way to do the same?

Also, is there a way to do the same using Criteria API?


Top
 Profile  
 
 Post subject: Re: Hibernate Query for missing one to one association
PostPosted: Mon Jan 16, 2012 11:52 am 
Beginner
Beginner

Joined: Wed Nov 21, 2007 10:24 am
Posts: 25
Is your association bi-directional? If it is, you can check the nullness of the mappedBy instance in ClassA. Although that does end up generating sql much like you have (although I believe they end up comparing a count to 0), but it's shorter for you to write.


Top
 Profile  
 
 Post subject: Re: Hibernate Query for missing one to one association
PostPosted: Tue Jan 17, 2012 4:37 am 
Newbie

Joined: Sat Jan 14, 2012 7:12 am
Posts: 7
Hi kjordan2001,

Thanks for your reply.

I had tried that.

Unfortunately, it does not return any result.

Can you please guide me on how the query should like with an example.

Regards,


Shrivallabh


Top
 Profile  
 
 Post subject: Re: Hibernate Query for missing one to one association
PostPosted: Tue Jan 17, 2012 10:25 am 
Beginner
Beginner

Joined: Wed Nov 21, 2007 10:24 am
Posts: 25
shrivallabh wrote:
Hi kjordan2001,

Thanks for your reply.

I had tried that.

Unfortunately, it does not return any result.

Can you please guide me on how the query should like with an example.

Regards,


Shrivallabh

For HQL/JPQL:
Code:
Query query = getSession().createQuery("from ClassA as ca where ca.classB is null");
query.list();


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