Link to home
Start Free TrialLog in
Avatar of rameshsukka
rameshsukka

asked on

Restriction class usage in Hibernate

Hi,

I am new Hibernate. Can someone help me to resolve the following issue,
I have two table with many to many relations, Product table and Group Table. Let me provide sample data

GroupId=1 Group Name= Calvin Klein Contains Product with Ids (10,11,12)
GroupId=2 Group Name= Shorts Contains Products with Ids (11,12,13,15)
GroupId=3 Group Name=Swimmers Contains products with Ids (12,17)
GroupId43 Group Name=caps Contains products with Ids (11,19,17)


I wanted to select the groups which contains common product which is also available in groups Calvin Klein and Shorts.  So my Hibernate query should pick common products in groups Calvin Klein and Shorts i.e 12 and find out other group which contains product 12 i.e group name Swimmers.

I have written the Hibernate code for the same but it is returning Group 3,4 and it is not selecting common products in Group 1 and 2.
     
String s[]={Calvin Klein, Shorts};
  DetachedCriteria detachedCriteriaForNarrowBy = DetachedCriteria.forClass(GroupDO.class, "group")
        .createAlias("group.productList", "product")
        .createAlias("product.groupList", "rootGroup")
        .add(Restrictions.ne("group.name"," Calvin Klein "))
.add(Restrictions.ne("group.name"," Shorts "))
        .add(Restrictions.in("rootGroup.name",s));
     
       detachedCriteriaForNarrowBy.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
        Criteria hibernateCriteria = detachedCriteriaForNarrowBy.getExecutableCriteria(session);

Can someone help to put Restriction to select only common products in above query.

Tnx,
Sukka
ASKER CERTIFIED SOLUTION
Avatar of rameshsukka
rameshsukka

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial