-->
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 Queries
PostPosted: Tue Dec 01, 2009 6:05 pm 
Beginner
Beginner

Joined: Fri Nov 13, 2009 4:05 pm
Posts: 30
I have the following tables:

ITEMS
ITEM_ID
PLANT_ID
ITEM_NUMBER

ITEM_MFG_PART_NUMBERS
ITEM_ID
PLANT_ID
MANUFACTURER_ID
MFG_PART_NUMBER

MANUFACTURERS
MANUFACTURER_ID
MANUFACTURER_NAME

From an entity point of view I have an Item, ItemMfgPartNumber, and Manufacturer objects where the Item contains a collection of ItemMfgPartNumber objects and these ItemMfgPartNumber objects contain a property to Manufacturer.

I need to be able to retreive all Item objects where ITEM_NUMBER contains '1' OR where the Item object's MFG_PART_NUMBER contains 'Q'.

Can someone show me how to create this via Criteria?


Top
 Profile  
 
 Post subject: Re: Criteria Queries
PostPosted: Wed Dec 16, 2009 1:29 pm 
Newbie

Joined: Wed Dec 16, 2009 12:24 pm
Posts: 6
Location: Munich
This means you want to create a restriction via a collection. Therefore you have to create a criteria on a criteria.

Code:
Criteria criteria = session.createCriteria(Item.class);
criteria.createCriteria("itemMfgPartNumbers", "numbers" ,CriteriaSpecification.INNER_JOIN );
criteria.add(Restrictions.like("item_number", "1", MatchMode.ANYWHERE);
criteria.add(Restrictions.like("numbers.mfg_part_number", "Q", MatchMode.ANYWHERE);
criteria.list();


To create aliases like "numbers" dynamically take a look at http://blog.bambo.it/2009/12/generic-subcriterias-for-hibernate.html


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.