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.