Criteria Query EntityManager - java

how can I write criteria query for below PL/Sql code?
SELECT a.eqis_type, a.eqis_des, a.eqis_rate
FROM eqis a
WHERE a.eqis_date IN (SELECT MAX (b.eqis_date)
FROM eqis b
WHERE b.eqis_type = a.eqis_type)
ORDER BY eqis_type;

Related

How to do a subquery on the same table by using JPA Hibernate?

Help me to convert the below mentioned query to Hibernate JPA.
Query query= entityManager.createQuery("Select a,b,c,d from table1 where d IN (Select d from table1 where a=1 and b=2");
Just giving an alias should work
entityManager.createQuery("Select a,b,c,d from table1 t
where t.d IN (Select r.d from table1 r where r.a=1 and r.b=2");

How to use `from` sub query in JPA QUERY DSL?

I have a query sql:
select distinct a.* from (select * from product_sku) a, (select #rowno:= 0) t;
Now, I want to perfrom my sql in query dsl:
jpaQuery.selectDistinct(qProductSku).from(query, jpaQuery.select(Expressions.numberTemplate(Integer.class, "#rowno:=0")));
I have already named a with query. Its type is JPAQuery<ProductSkuEntity>. The type of jpaQuery is JPAQueryFactory.
But it is incorrect, because from method only accpt type EntityPath.
How could I transfrom it to query dsl correctly?
(Don't ask me why this SQL is so strange, it just a part of complete expression)
SQLQuery sqlQuery = new SQLQuery(connection, PostgresTemplates.builder().quote().newLineToSingleSpace()
.printSchema().build());
ListSubQuery<Tuple> listSubQuery = new SQLSubQuery().from(QUsersPasswords.usersPasswords).orderBy(QUsersPasswords.usersPasswords.usuNummat.desc()).list(QUsersPasswords.usersPasswords.all());
QUsersPasswords qSubquery = new QUsersPasswords("subquery");
sqlQuery.from(listSubQuery.as("subquery")).limit(10);
sqlQuery.list(qSubquery.all());

Hibernate Criteria - Excluding group by clause in the select statement

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

Add count of child table to select from parent with hibernate criteria API

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

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

Categories