-->
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.  [ 2 posts ] 
Author Message
 Post subject: Criteria query: one2many problem
PostPosted: Fri Jul 27, 2007 4:56 am 
Newbie

Joined: Thu May 17, 2007 12:19 pm
Posts: 2
Hi,

Lets say I have 3 tables (position, item, subitem).
A position has 1..* items, and an item has 1..* subitems.

Now I wanna search for all positions with subitem.description like "xyz".

Code:
Session session = HibernateUtil.getSessionFactory().openSession();
criteria = session.createCriteria(Position.class, "pos");
criteria.createAlias("pos.items.subitems", "subitem", CriteriaSpecification.LEFT_JOIN);
criteria.add(Restrictions.like("subitem.description", "%xyz%"));


That way I get the exception:
Unknown column 'subitem1_.Description' in 'where clause'.

But if I do first:

Code:
criteria.createAlias("pos.items", "item", CriteriaSpecification.LEFT_JOIN);
criteria.add(Restrictions.like("item.description", "%%"));


and then

Code:
criteria.createAlias("pos.items.subitems", "subitem", CriteriaSpecification.LEFT_JOIN);
criteria.add(Restrictions.like("subitem.description", "%xyz%"));


it works fine.



Any ideas?

Thanks Bona


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 27, 2007 6:18 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
Try creating sub criteria for your joined tables instead of aliases. For me, its a more intuitive format:

Code:
Session session = HibernateUtil.getSessionFactory().openSession();
criteria = session.createCriteria(Position.class);
criteria.createCriteria("items", CriteriaSpecification.LEFT_JOIN)
.createCriteria("subitems", CriteriaSpecification.LEFT_JOIN)
.add(Restrictions.like("description", "%xyz%"));


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