converting inner query using Hibernate Criteria - java

I'm trying to convert below nested query into Hibernate Criteria, but not able to do.
Actually, trying to count and sum the rows from the result set.
so anybody have any ideas ?
Thanks in advance.
SELECT DISTINCT host_platform,
host_manufacturer,
COUNT(phy_1),
COUNT (vir_1),
SUM (server_cost) AS server_cost,
SUM (book_value) AS book_value,
SUM (maintenance_cost) AS main_cost,
SUM (current_cost) AS curr_cost
FROM (SELECT DISTINCT host_platform,
host_manufacturer,
phy_1,
vir_1,
server_cost,
book_value,
maintenance_cost,
current_cost
FROM tf_server_list_v
where host_platform = 'UNIX')
--ipaddress = '142.125.21.70')
GROUP BY host_platform, host_manufacturer;
Here below is the criteria written for above query,
For select main query,
Criteria mainCriteria = getSession().createCriteria(TfServerListNew.Class);
ProjectionList projOSList1 = Projections.projectionList();
projOSList1.add(Projections.property("hostPlatform"), "hostPlatform");
ProjectionList projectionList1 = Projections.projectionList();
projectionList1.add(Projections.distinct(projOSList));
projectionList1.add(Projections.alias(Projections.property("hostManufacturer"), "hostManufacturer"));
projectionList1.add(Projections.alias(Projections.count("physicalFlag"), "physical"));
projectionList1.add(Projections.alias(Projections.count("virtualFlag"), "virtual"));
projectionList1.add(Projections.alias(Projections.sum("serverCost"), "serverCost"));
projectionList1.add(Projections.alias(Projections.sum("bookValue"), "bookValue"));
projectionList1.add(Projections.alias(Projections.sum("mainCost"), "mainCost"));
projectionList1.add(Projections.alias(Projections.sum("currCost"), "currCost"));
mainCriteria.setProjection(projectionList1);
For inner query to populate result set and pass to main query,
final DetachedCriteria criteria = DetachedCriteria.forClass(TfServerListNew.class);
ProjectionList projOSList = Projections.projectionList();
projOSList.add(Projections.property("hostPlatform"), "hostPlatform");
ProjectionList projectionList = Projections.projectionList();
projectionList.add(Projections.distinct(projOSList));
projectionList.add(Projections.alias(Projections.property("hostManufacturer"), "hostManufacturer"));
projectionList.add(Projections.alias(Projections.property("physicalFlag"), "physical"));
projectionList.add(Projections.alias(Projections.property("virtualFlag"), "virtual"));
projectionList.add(Projections.alias(Projections.property("serverCost"), "serverCost"));
projectionList.add(Projections.alias(Projections.property("bookValue"), "bookValue"));
projectionList.add(Projections.alias(Projections.property("mainCost"), "mainCost"));
projectionList.add(Projections.alias(Projections.property("currCost"), "currCost"));
But how to pass this subquery to main query is not getting.

Use
mainCriteria.add(Restrictions.sqlRestriction("your nested query"));
or you can use other versions of sqlRestriction based on requirement
I think This method is good one.Or as you have not provided details no answer related to your code.

Related

Hibernate sum and group

everybody:
So, I have this system created with Hibernate and Spring, and I want to create Reports to see the total piece and the total sold per object, but I'm having troubles to understand how to create this Mysql query to hibernate criteria.
This is my query:
SELECT
SUM(laborderdetail.total) AS suma,
sum(laborderdetail.quantity) AS total,
laborderdetail.`itemMaster_id` AS total,
itemmaster.`ITEMNAME` AS total,
udc.`STRVALUE1` AS total
FROM
`laborderdetail` laborderdetail INNER JOIN `itemmaster` itemmaster ON laborderdetail.`itemMaster_id` = itemmaster.`id`
INNER JOIN `udc` udc ON itemmaster.`linetype_id` = udc.`id`
INNER JOIN `laborderdetail` laborderdetail_A ON itemmaster.`id` = laborderdetail_A.`itemMaster_id`
INNER JOIN `laborder` laborder ON laborderdetail_A.`labOrder_id` = laborder.`id`
INNER JOIN `laborder_laborderdetail` laborder_laborderdetail ON laborderdetail_A.`id` = laborder_laborderdetail.`labOrderDetail_id`
AND laborder.`id` = laborder_laborderdetail.`LABORDER_id`
AND laborderdetail.`id` = laborder_laborderdetail.`labOrderDetail_id`
AND laborder.`id` = laborderdetail.`labOrder_id`
AND udc.`id` = laborder.`rate_id`
AND udc.`id` = laborder.`priceList_id`
AND udc.`id` = laborder.`orderReference_id`
WHERE laborder.`company_id` = 1
GROUP BY
laborderdetail.itemMaster_id
ORDER BY
udc.STRVALUE1 ASC
I know I have to use criteria.setFetchMode to JOIN all my tables and then use ProjectionList for my SUM, but the way the system is programmed makes me confused cause in my model of LabOrder I have this code
#OneToMany(fetch=FetchType.EAGER, cascade=CascadeType.ALL)
private Set<LabOrderDetail> labOrderDetail;
So when I try this code:
DetachedCriteria criteria = DetachedCriteria.forClass(LabOrder.class);
criteria.setFetchMode("laborderdetail", FetchMode.JOIN);
if (this.companyId != 0)
criteria.add(Restrictions.eq("company.id", this.companyId));
ProjectionList projectionList = Projections.projectionList();
projectionList.add(Projections.groupProperty("laborderdetail.itemMaster_id"));
projectionList.add(Projections.sum("laborderdetail.total"));
projectionList.add(Projections.sum("laborderdetail.quantity"));
criteria.setProjection(projectionList);
criteria.addOrder(Order.asc("laborderdetail.itemMaster_id"));
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
List<LabOrder> list = hibernateTemplate.findByCriteria(criteria);
I got this error with my line projectionList.add(Projections.groupProperty("laborderdetail.itemMaster_id"));
This is the error:
could not resolve property: laborderdetail.itemMaster_id of: com.gds.model.LabOrder
I know is cause itemMaster_id is in my model of laborderdetail, but how can I do my query?
Maybe it's silly thing but I don't have any clue since I'm not that familiarize with hibernate.
Thanks.

How to build a query using Hibernate Criteria and Projections

I want to build a query like below using Hibernate Projections attribute. Can someone check the below. I have written java code like.
DetachedCriteria dCriteria = DetachedCriteria.forClass(FinancialYearQuater.class, "FinancialYearQuater");
dCriteria.add(Restrictions.eq("FinancialYearQuater.finYear", year));
dCriteria.addOrder(Order.asc("finYear"));
dCriteria.setResultTransformer(Projections.distinct(Projections.property("id")));
List<FinancialYearQuater> list = (List<FinancialYearQuater>) findAll(dCriteria);
Here's the SQL query:
select
distinct
this_.FINY_NAME,
this_.FINY_YEAR,
this_.QTR_NAME,
this_.QTR_NO,
this_.QTR_PERIOD
from
V_FINYR_QTR this_
where
this_.FINY_YEAR=2016
order by
this_.FINY_YEAR asc
I have written below code. Is that the correct way get the distinct data?
DetachedCriteria dCriteria = DetachedCriteria.forClass(FinancialYearQuater.class, "FinancialYearQuater");
dCriteria.add(Restrictions.eq("FinancialYearQuater.finYear", year));
ProjectionList projList = Projections.projectionList();
projList.add(Projections.property("FinancialYearQuater.finYear"), "finYear");
projList.add(Projections.property("FinancialYearQuater.finYearName"), "finYearName");
projList.add(Projections.property("FinancialYearQuater.qtrNo"), "qtrNo");
projList.add(Projections.property("FinancialYearQuater.qtrPeriod"), "qtrPeriod");
dCriteria.setProjection(Projections.distinct(projList));
dCriteria.addOrder(Order.asc("finYear"));
List<FinancialYearQuater> list = (List<FinancialYearQuater>) findAll(dCriteria);

Hibernate Criteria in Clause with 2 conditions in 1 Query

How do i acheive the following SQL Query using Hibernate Criteria Query:
select * from Table1 where (A,B) in (select A,B from Table2)
Assuming we have Criteria for Table 1 and Detached Criteria for Table 2. This below code work flawlessly:
Criteria criteria = new Criteria(Table1.class);
DetachedCriteria dc = DetachedCriteria.forClass(Table2.class);
ProjectionList projList = Projections.projectionList();
projList.add(Projections.property("column1"));
projList.add(Projections.property("column2"));
dc.setProjection(Projections.distinct(projList));
criteria.add(Subqueries.propertiesIn(new String[]{"column1","column2"}, dc));

Self-Join using Hibernate Criteria

I'm trying to reproduce a SQL query containing self-joins using Hibernate. This is the SQL:
select cur.*
from first ft1,
first ft2,
second sc1,
second sc2,
third thd
where sc1.id = sc2.id
and sc1.idFirst = ft1.id
and sc2.idFirst = ft2.id
and ft1.is <> ft2.id
and ft1.id = thd.idFirst
and ft2.id = ?
I've tried using Criteria and DetachedCriteria as follows, by I'm not able to get it to work:
Criteria criteria = this.getSession().createCriteria(Third.class);
DetachedCriteria first1Criteria = DetachedCriteria.forClass(First.class);
first1Criteria.setProjection(Projections.property("id"));
DetachedCriteria first2Criteria = DetachedCriteria.forClass(First.class);
first2Criteria.setProjection(Projections.property("id"));
first2Criteria.add(Restrictions.eq("id", id)); // where id is passed in from the calling method
first2Criteria.getExecutableCriteria(getSession()).list();
DetachedCriteria second1Criteria = DetachedCriteria.forClass(Second.class);
second1Criteria.setProjection(Projections.property("id"));
DetachedCriteria second2Criteria = DetachedCriteria.forClass(Second.class);
second2Criteria.setProjection(Projections.property("id"));
second1Criteria.add(Property.forName("id.id").in(second2Criteria ));
second1Criteria.add(Property.forName("id.idFirst").in(first1Criteria));
second1Criteria.add(Property.forName("id.idFirst").in(first2Criteria));
first1Criteria.add(Property.forName("id").notIn(first2Criteria));
criteria.add(Property.forName("id").in(first1Criteria));
return criteria.list();
No exceptions are thrown in this case, but the query isn't actually executed. I've tried a few differnt combinations along these lines, but unsuccessfully. Any help is much apreciated!

Converting the following criteria into SQL

I have the following criteria that I am using,..
List<boop> books =null;
Criteria criteria = session.createCriteria(boop.class);
ProjectionList proList = Projections.projectionList();
proList.add(Projections.property("AAA"));
proList.add(Projections.property("BBB"));
RETURN criteria.list();
please advise how can I convert it in HQL..
Have a look at the Projections javadoc. Each Projections property corresponds to a database field.
select b.AAA, b.BBB
from boop b

Categories