Self-Join using Hibernate Criteria - java

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!

Related

How to Write Simple Hibernate Criteria Subquery

How can I write this SQL query as a Hibernate JPA Criteria (with Restrictions, etc) in Java ?
SELECT q.*
FROM queue AS q
WHERE q.executed = false AND
q.queued_on = (SELECT min(queued_on) FROM queue WHERE item_id = q.item_id);
I only managed to write the first part like this:
getBaseCriteria()
.add(Restrictions.eq("executed", false))
// Missing Second Where Filter Here
.addOrder(Order.asc("queuedOn"))
.list();
Try to create a separate criteria instance for the subquery and simply add as another restriction as follows:
DetachedCriteria subCriteria = DetachedCriteria.forClass(Queue.class, "sub")
.add(Restrictions.eqProperty("sub.itemId","main.itemId"))
.setProjection(Projections.projectionList().add(Projections.min("sub.queuedOn")));
session.createCriteria(Queue.class, "main")
.add(Subqueries.propertyEq("main.queuedOn", subCriteria ));
.add(Restrictions.eq("main.executed", false));
.addOrder(Order.asc("main.queuedOn"))
.list();

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);

converting inner query using Hibernate Criteria

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.

criteria query ORDER BY yields error. Is this an SQL-SERVER limitation? How could I order by correctly on a complicated criteria query?

I have the following criteria query:
String cat = "H";
Criteria criteria = currentSession().createCriteria(this.getPersistentClass()).
add(Restrictions.ne("category", cat)).
createAlias("employees", "emp").
createAlias("emp.company", "company");
Disjunction disjunction = Restrictions.disjunction();
for(Region r: regions){
disjunction.add(Restrictions.eq("company.region", r));
}
criteria.add(disjunction);
if(status != null) {
criteria.add(Restrictions.eq("status", status));
}
if (period != null) {
criteria.add(Restrictions.eq("period", period));
}
criteria.setProjection(Projections.groupProperty("id")) //this line was added to try to "fix" the error, but it still happened.
criteria.addOrder(Order.asc("id"));
I guess a query that explains my criteria query could be:
select n.* from NOMINATION n
join NOMINEE i on n.NOM_ID = i.NOM_ID
join EMPLOYEE e on e.EMP_ID = i.EMP_ID
join COMPANY c on c.COMPANY_CODE = e.COMPANY_CODE
where n.CATEGORY_CODE!='H' and (c.REGION_ID = ? or c.REGION_ID = ? or c.REGION_ID = ?) and n.STATUS_ID = ? and n.PERIOD_ID = ?
order by n.NOM_ID
What I am trying to do here, is pretty confusing but for the most part it works except when I add this specific line (though the query works fine):
criteria.addOrder(Order.asc("id"));
and then I get error:
java.sql.SQLException: Column "NOMINATION.NOM_ID" is invalid in the ORDER BY clause because it is not contained in either an aggregate function or the GROUP BY clause.
Which I suspect is something that has to do with SQL-SERVER. I am already grouping by id. So what am I doing wrong here, or should I just use HQL?
Your current query seems to be a simple Query which doesn't have any group function used or not a group by query. According to your current requirements you do not have to use this line.
criteria.setProjection(Projections.groupProperty("id")).addOrder(Order.asc("id"));
Or you have to modify your sql statements.

Hibernate Criteria - Exclude groupProperty from select

I would like to use a hibernate criteria object as a subquery on a second criteria, like this:
DetachedCriteria latestStatusSubquery = DetachedCriteria.forClass(BatchStatus.class);
latestStatusSubquery.setProjection(Projections.projectionList()
.add( Projections.max("created"), "latestStatusDate")
.add( Projections.groupProperty("batch.id"))
);
DetachedCriteria batchCriteria = DetachedCriteria.forClass(BatchStatus.class).createAlias("batch", "batch");
batch.add( Property.forName( "created" ).eq( latestStatusSubquery ) );
The problem is that adding a groupProperty automatically add that property to the select clause on the subselect query and I can't find any way to stop this from happening.
The result, of course a DB error because the subquery returns too many values.
Does anyone know a way around this?
Try like below sample,
DetachedCriteria subquery = DetachedCriteria.forClass(CustomerCommentsVO.class, "latestComment");
subquery.setProjection(Projections.max("latestComment.commentId"));
subquery.add(Expression.eqProperty("latestComment.prospectiveCustomer.prospectiveCustomerId", "comment.prospectiveCustomer.prospectiveCustomerId"));
objCriteria = objSession.createCriteria(CustomerCommentsVO.class,"comment");
objCriteria.add(Subqueries.propertyEq("comment.commentId", subquery));
List lstComments = objCriteria.list();

Categories