-->
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: Help with Criteria query and several associations
PostPosted: Thu May 04, 2006 1:37 pm 
Newbie

Joined: Thu May 04, 2006 11:06 am
Posts: 1
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:3.1.1


Hi All,

I want execute a query with several join with the Criteria API but I don't have the same result than a HQL query.

see below the code with HQL query
Code:
        Query query = getSession()
        .createQuery(
                "select xcaf " +
                "from XCompanyAttributeField xcaf " +
                "join xcaf.company comp " +
                "join xcaf.XAttribute as xatt " +
                "join xatt.serviceList as service " +
                "where service.code = :code " +
                "and comp.companyId = :companyId ");

        query.setLong("companyId",company.getCompanyId());
        query.setInteger("code",subscription.getService().getCode());
        List<XCompanyAttributeField> xcafList = query.list();


This HQL query generate the SQL:
Code:
select xcompanyat0_.XCompanyAttributeFieldId as XCompany1_46_, xcompanyat0_.FK_XCAFCompanyId as FK2_46_, xcompanyat0_.FK_XCAFAttributeId as FK3_46_, xcompanyat0_.FK_XCAFFieldId as FK4_46_ from TXCompanyAttributeField xcompanyat0_ inner join TCompany company1_ on xcompanyat0_.FK_XCAFCompanyId=company1_.CompanyId inner join TXAttribute xattribute2_ on xcompanyat0_.FK_XCAFAttributeId=xattribute2_.XAttributeId inner join TServiceXAttribute servicelis3_ on xattribute2_.XAttributeId=servicelis3_.FK_XAttributeId inner join TService service4_ on servicelis3_.FK_ServiceId=service4_.ServiceId where service4_.Code=? and company1_.CompanyId=?


In this case, I extract only the properties of XCompanyAttributeField object.

I want the same with the Criteria API.
I execute the code below:

Code:
Session session = this.getSession();
Criteria crit = session.createCriteria(XCompanyAttributeField.class, xcaf");
crit.add(Restrictions.eq("xcaf.company",company))
.createAlias("XAttribute","xatt")
.createAlias("xatt.serviceList","service")
.add(Restrictions.eq("service.code",subbscription.getService().getCode()));
       
List<XCompanyAttributeField> xcafList = crit.list();


This Criteria query generate the SQL below:
Code:
select this_.XCompanyAttributeFieldId as XCompany1_46_2_, this_.FK_XCAFCompanyId as FK2_46_2_, this_.FK_XCAFAttributeId as FK3_46_2_, this_.FK_XCAFFieldId as FK4_46_2_, xatt1_.XAttributeId as XAttribu1_43_0_, xatt1_.Code as Code43_0_, xatt1_.Description as Descript3_43_0_, xatt1_.Name as Name43_0_, xatt1_.MonitoringName as Monitori5_43_0_, xatt1_.FK_XAttributeValueTypeId as FK6_43_0_, servicelis5_.FK_XAttributeId as FK2_, service2_.ServiceId as FK1_, service2_.ServiceId as ServiceId29_1_, service2_.Name as Name29_1_, service2_.Code as Code29_1_, service2_.Description as Descript4_29_1_, service2_.FK_CategoryId as FK5_29_1_ from TXCompanyAttributeField this_ inner join TXAttribute xatt1_ on this_.FK_XCAFAttributeId=xatt1_.XAttributeId inner join TServiceXAttribute servicelis5_ on xatt1_.XAttributeId=servicelis5_.FK_XAttributeId inner join TService service2_ on servicelis5_.FK_ServiceId=service2_.ServiceId where this_.FK_XCAFCompanyId=? and service2_.Code=?

In this case, I extract all the properties of all objects (XCompanyAttributeField, XAttribute, Service).

How can I extract only the XCompanyAttributeField properties with criteria API.

Thanks for your help.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 04, 2006 5:18 pm 
Expert
Expert

Joined: Tue Apr 25, 2006 12:04 pm
Posts: 260
Will this work for you,

Code:
DetachedCriteria dc1 = DetachedCriteria.forClass( Service.class, "service" );
dc1.add( Restrictions.eq( "service.code", subbscription.getService().getCode() ) );
dc1.setProjection( Property.forName( "service.commonProperty" ) );

DetachedCriteria dc2 = DetachedCriteria.forClass( XAttribute.class, "xatt" );
dc2.setProjection( Property.forName( "xatt.commonProperty" ) );

DetachedCriteria c = DetachedCriteria.forClass( XCompanyAttributeField.class, "xcaf" );
c.add( Restrictions.eq("xcaf.company", company )) ;
c.add( Property.forName( "xcaf.commonProperty" ).eq( dc1 ) );
c.add( Property.forName( "xcaf.commonProperty" ).eq( dc2 ) );

List results = c.getExecutableCriteria( session ).list();


The result should contain only information for the object XCompanyAttributeField.


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.