I've written this in sql server:
select max(processed_trans_id) from EQUITY_TRANSACTION where transaction_date <= '2016-06-22' and company_id=75 group by comp_acct_id
But when I try to write the same in Hibernate Criteria, like this:
DetachedCriteria dCriteria = DetachedCriteria.forClass(EquityTransaction.class)
.add(Restrictions.eq("clientCompany.id", sb.getClientCompany().getId()))
.add(Restrictions.le("transactionDate", sb.getQualifyDate()))
.setProjection(Projections.projectionList()
.add(Projections.max("processedTransaction.id"), "processedTransaction.id")
.add(Projections.groupProperty("holderCompanyAccount.id")));
I get this as the sql query from hibernate:
select max(processed_trans_id), comp_acct_id from EQUITY_TRANSACTION where transaction_date <= '2016-06-22' and company_id=75 group by comp_acct_id
How do I replicate the original SQL query in Hibernate criteria?
I was able to resolve this using the following query. Worked like a charm!
DetachedCriteria dCriteriaEt1 = DetachedCriteria.forClass(EquityTransaction.class, "et1")
.add(Restrictions.eq("clientCompany.id", decldDvd.getClientCompany().getId()))
.add(Restrictions.le("transactionDate", decldDvd.getQualifyDate()))
.setProjection(Projections.projectionList()
.add(Projections.max("processedTransaction.id"), "processedTransaction.id"))
.add(Restrictions.eqProperty("et1.holderCompanyAccount.id", "et2.holderCompanyAccount.id"));
logger.info("Retrieving all qualified company account balances...");
Criteria getQualifiedBalances = dao.getSession().createCriteria(EquityTransaction.class, "et2")
.add(Subqueries.propertyIn("processedTransaction.id", dCriteriaEt1))
.add(Restrictions.gt("balance", 0l))
.setProjection(Projections.projectionList()
.add(Projections.property("holderCompanyAccount.id"),"holderCompanyAccount.id")
.add(Projections.property("balance"), "balance"))
.addOrder(Order.asc("holderCompanyAccount.id"))
.addOrder(Order.desc("processedTransaction.id"))
.setResultTransformer(new AliasToBeanNestedResultTransformer(EquityTransaction.class));
Related
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);
How can I execute this SQL query using the Hibernate Criteria API?
SELECT c.*, count(r.id) FROM COURSE c left join REFERRAL r on c.id = r.course_id group by c.id
Something like this seems close enough:
Criteria criteria = currentSession().createCriteria(Referral.class, "r");
criteria.createAlias("r.course", "course");
ProjectionList projections = Projections.projectionList();
projections.add(Projections.groupProperty("course.id"));
projections.add(Projections.count("r.id"));
return list(criteria
.setProjection(projections));
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));
I need to do this query with Java + Hibernate.
SELECT
table2.id,
COUNT(table2.id) AS count
FROM
table1
JOIN table2
ON table1.fk_tb2 = table2.id --many2one
GROUP BY
table2.id
I would use DetachedCriteria class.....how can i do this ?
Try using projections like this:
Criteria table1Crit = session.createCriteria("table1");
Criteria table2Crit = table1Crit.createCriteria("table2", Criteria.INNER_JOIN);
table2Crit.setProjection( Property.forName("id").count() );
use your query with createNativeQuery method
java experts can you please help me write detached queries as a part of the criteria query for the following SQL statement.
select A.*
FROM AETABLE A
where not exists
(
select entryid
FROM AETABLE B
where B.classpk = A.classpk
and B.userid = A.userid
and B.modifiedDate > A.modifiedDate
)
and userid = 10146
You need to write a correlated subquery. Assuming property / class names match column / table names above:
DetachedCriteria subquery = DetachedCriteria.forClass(AETable.class, "b")
.add(Property.forName("b.classpk").eqProperty("a.classpk"))
.add(Property.forName("b.userid").eqProperty("a.userid"))
.add(Property.forName("b.modifiedDate").gtProperty("a.modifiedDate"));
Criteria criteria = session.createCriteria(AETable.class, "a")
.add(Property.forName("userid").eq(new Integer(10146)))
.add(Subqueries.notExists(subquery);
Just one addition to the above query. If the entryid is not the primary key, then you'll need to add projection.
DetachedCriteria subquery = DetachedCriteria.forClass(AETable.class, "b")
.add(Property.forName("b.classpk").eqProperty("a.classpk"))
.add(Property.forName("b.userid").eqProperty("a.userid"))
.add(Property.forName("b.modifiedDate").gtProperty("a.modifiedDate"))
.add(setProjection(Projections.property("entryId")); // Additional projection property
Criteria criteria = session.createCriteria(AETable.class, "a")
.add(Property.forName("userid").eq(new Integer(10146)))
.add(Subqueries.notExists(subquery);