-->
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.  [ 9 posts ] 
Author Message
 Post subject: many to many query with criteria
PostPosted: Thu Jan 15, 2015 2:40 am 
Newbie

Joined: Fri Jan 09, 2015 4:56 am
Posts: 9
hi, guys, I have two tables with many-to-many relationships which called Position and Content.

Mapping is collect, because insert and and other queries all can work properly.


and I use

Criteria c = sessionFactory.getCurrentSession().createCriteria(Content.class);
c.add(Restrictions.isEmpty("position"));

to get the dataset which means all the Contents are not taken by any Position.

It works properly.

And then, I add:

Criteria c = sessionFactory.getCurrentSession().createCriteria(Content.class);
c.add(Restrictions.isEmpty("position"));
c.createAlias("position", "p");
c.add(Restrictions.eq("p.id", 100));

which means that to get all the Contents are not taken by Position 100.

I get a empty return.

May I know what I was wrong in the case please?


Top
 Profile  
 
 Post subject: Re: many to many query with criteria
PostPosted: Thu Jan 15, 2015 11:29 pm 
Newbie

Joined: Fri Jan 09, 2015 4:56 am
Posts: 9
Up and thank you!! Please help~


Top
 Profile  
 
 Post subject: Re: many to many query with criteria
PostPosted: Fri Jan 16, 2015 2:05 am 
Newbie

Joined: Mon Jan 12, 2015 3:56 am
Posts: 5
Please, write SQL-query, that you want to get.

I think, you need this:
Code:
c.createAlias("position", "p");
c.add(Restrictions.not(Restrictions.eq("p.id", 100)))


(deleted first restriction and added "not" to last)


Top
 Profile  
 
 Post subject: Re: many to many query with criteria
PostPosted: Fri Jan 16, 2015 4:00 am 
Newbie

Joined: Fri Jan 09, 2015 4:56 am
Posts: 9
astafev wrote:
Please, write SQL-query, that you want to get.

I think, you need this:
Code:
c.createAlias("position", "p");
c.add(Restrictions.not(Restrictions.eq("p.id", 100)))


(deleted first restriction and added "not" to last)



Thanks a lot! It works!!


Top
 Profile  
 
 Post subject: Re: many to many query with criteria
PostPosted: Mon Jan 19, 2015 2:53 am 
Newbie

Joined: Fri Jan 09, 2015 4:56 am
Posts: 9
astafev wrote:
Please, write SQL-query, that you want to get.

I think, you need this:
Code:
c.createAlias("position", "p");
c.add(Restrictions.not(Restrictions.eq("p.id", 100)))


(deleted first restriction and added "not" to last)



The problem is so interesting, that the code just work once time correctly, now it can only get result which equals

Restrictions.eq("p.id", 100)

but Restrictions.not() looks like no function anymore.

Any ideas?


Here is the SQL I need to query:

SELECT * FROM `content` WHERE `content`.id not in (select content_id from `position_has_content` where position_id = 62)


but I think

Criteria c = sessionFactory.getCurrentSession().createCriteria(Content.class);
c.createAlias("position", "p");
c.add(Restrictions.not(Restrictions.eq("p.id", 62)));


results:

SELECT * FROM `position_has_content` WHERE content_id not in (select content_id from `position_has_content` where position_id = 62)


it is quite different.


Top
 Profile  
 
 Post subject: Re: many to many query with criteria
PostPosted: Mon Jan 19, 2015 11:53 pm 
Newbie

Joined: Mon Jan 12, 2015 3:56 am
Posts: 5
The problem is that you get select from wrong table, right? Write, please, mapping of your classes and query, that Hibernate writes in log (with "this_" and etc)


Top
 Profile  
 
 Post subject: Re: many to many query with criteria
PostPosted: Sun Jan 25, 2015 10:17 pm 
Newbie

Joined: Fri Jan 09, 2015 4:56 am
Posts: 9
astafev wrote:
The problem is that you get select from wrong table, right? Write, please, mapping of your classes and query, that Hibernate writes in log (with "this_" and etc)


thank you for helping
but I think I already select from the right target I want:

Criteria c = sessionFactory.getCurrentSession().createCriteria(Content.class);
c.add(Restrictions.isEmpty("position"));
c.createAlias("position", "p");
c.add(Restrictions.not(Restrictions.eq("p.id", 100)));

The thing many be that firstly I used EAGER to search from join-table to find which already exists, and the final result actually comes from the single table Content which should use LAZY to get result.


Top
 Profile  
 
 Post subject: Re: many to many query with criteria
PostPosted: Sun Jan 25, 2015 10:31 pm 
Newbie

Joined: Fri Jan 09, 2015 4:56 am
Posts: 9
Criteria c = sessionFactory.getCurrentSession().createCriteria(Content.class);
c.add(Restrictions.isEmpty("position"));
c.createAlias("position", "p");
c.add(Restrictions.not(Restrictions.eq("p.id", 100)));

works like below:

SELECT * FROM `position_has_content` WHERE content_id not in (select content_id from `position_has_content` where position_id = 100)



but the result I want should like this:
SELECT * FROM `content` WHERE `content`.id not in (select content_id from `position_has_content` where position_id = 100)


Top
 Profile  
 
 Post subject: Re: many to many query with criteria
PostPosted: Tue Feb 03, 2015 6:55 am 
Newbie

Joined: Fri Jan 09, 2015 4:56 am
Posts: 9
[quote="xiaonvren"]The problem is that you get select from wrong table

I tried many way, the final solution looks like below:

Criteria c = sessionFactory.getCurrentSession().createCriteria(Content.class);
c.createAlias("position", "p");
c.add(Restrictions.eq("p.id", 100));

List<Content> nc = c.list();
List<Integer> idList = new ArrayList<Integer>();

for(Content cont:nc)
{
idList.add(cont.getId());
}

Criteria c2 = sessionFactory.getCurrentSession().createCriteria(Content.class).setFetchMode("position", FetchMode.SELECT);

if(nc.size()!=0) c2.add(Restrictions.not(Restrictions.in("id",idList)));

return c2.list();

I know it is not efficient and effective, so any ideas to help? thank you.


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