How can I implement Select and Count in Hibernate using criteria - java

I'm new using hibernate, I have query like :
select count(1) from (
SELECT COUNT (1)
FROM USR_BASE
WHERE ST_CD = 1
group by USR_NO)
How can I implement that query in Hibernate using criteria ?
Because, I already implement with method :
public int totalUser(UsrBase usrBase) {
Criteria criteria = createCriteria();
String stCd = usrBase.getStCd();
criteria.setProjection(Projections.projectionList())
.add(Projections.property("usrNo"))
.add(Projections.property(stCd))
.add(Projections.groupProperty("usrNo")));
return((Long)criteria.setProjection(Projections.rowCount()).uniqueResult()).intValue();
}
the result not same with my query... Please help me.

select count(1) from (
SELECT COUNT (1)
FROM USR_BASE
WHERE ST_CD = 1
group by USR_NO)
I think it will be more easily with
select count(distinct(USR_NO)) from USR_BASE WHERE ST_CD = 1

Related

SQL Syntax error in Hibernate's map function

SELECT NEW Map (PRODUCT_CATEGORY, COUNT(PRODUCT_CATEGORY) AS COUNTER) from Product WHERE USER_ID = (SELECT USER_ID FROM USERS WHERE USERNAME='burak123'
Hi everyone,
As hibernates document says here: https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/queryhql.html#queryhql-select
I am trying to map the query result into a hash map like this
#Query(value = "SELECT NEW Map( PRODUCT_CATEGORY , COUNT(PRODUCT_CATEGORY) AS COUNTER ) from Product WHERE USER_ID=(SELECT USER_ID FROM USERS WHERE USERNAME=(:username)) ",nativeQuery = true)
HashMap<Integer,Integer> getCategoryCountsWithUsername(#Param("username")String username);
But it throws an JdbcSyntaxErrorException. I am trying to solve this for like 1 hours already. Can someone help?
You are using a native query, not an HQL query. Check your SQL syntax.
With a native query, your named parameter won't work. You need to remove the nativeQuery = true

JPA limit subquery result (Jhipster)

I need to put
order by tabB.id desc
limit 1
in a subquery because the subquery must return a single value.
Long filter= Long.parseLong(value);
return (root, query, builder) -> {
Subquery<B> subquery = query.subquery(B.class);
Root<B> subqueryRoot = subquery.from(B.class);
Join<B,C> ss = subqueryRoot.join(B_.idC);
subquery.correlate(ss);
subquery.select(subqueryRoot.get(B_.ID_C));
subquery.where(
builder.equal(subqueryRoot.get(B_.idA),root.get(A_.id))
);
subquery.
builder.max(subqueryRoot.get(B_.id)); //first try
builder.desc(subqueryRoot.get(B_.id)); //another try
return builder.equal(subquery, filter);
};
adding "first try" and/or "another try" nothing changes in the query created, they are simply ignored and executing it I reach:
org.postgresql.util.PSQLException: ERROR: more than one row returned by a subquery used as an expression
Is there a way to take the first element in the subquery so can apply to the where query?
My situation is similar What are the alternatives to using an ORDER BY in a Subquery in the JPA Criteria API?
but I have a equals not over the id:
SELECT q.id_project FROM status q
WHERE q.status_name like 'new'
AND q.id IN (
SELECT TOP 1 sq.id from status sq
WHERE q.id_project = sq.id_project
ORDER BY sq.id DESC )
my situation is:
SELECT A.id_project FROM tabA A
WHERE A.col like 'alpha'
AND 'centauri' = (
SELECT TOP 1 B.colAA from tabB B
WHERE A.id_project = B.id_project
ORDER BY B.id DESC )

Create query in Criteria API

I'm using Hibernate Criteria API. Can you help me get a list of id's like this SQL query.
Here is the SQL query:
SELECT id FROM upload_rdp idx WHERE idx.is_update_stat = 0 AND
(SELECT COUNT(r_a.id) FROM rdp r_a WHERE r_a.upload_rdp_id = idx.id) =
(SELECT COUNT(r_cv.id) FROM rdp r_cv WHERE r_cv.upload_rdp_id = idx.id AND
r_cv.is_checked_valid = 1) ORDER BY idx.id;
Here is a code, where I get a list of the first query. The SQL query is:
SELECT id FROM upload_rdp idx WHERE idx.is_update_stat = 0;
Using the Criteria API:
Criteria criteria = createEntityCriteria().addOrder(Order.asc("id"));
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
criteria.add(Restrictions.eq("isUpdateStat", 0));
criteria.setProjection(Projections.property("id"));
criteria.list();
Thanks and sorry for my English.

How to write criteria eqivalent of sql having count [duplicate]

I need to create a query and I need COUNT(*) and HAVING COUNT(*) = x.
I'm using a work around that uses the CustomProjection class, that I downloaded somewhere.
This is the SQL that I try to achieve:
select count(*) as y0_, this_.ensayo_id as y1_ from Repeticiones this_
inner join Lineas linea1_ on this_.linea_id=linea1_.id
where this_.pesoKGHA>0.0 and this_.nroRepeticion=1 and linea1_.id in (18,24)
group by this_.ensayo_id
having count(*) = 2
This is the code, where I use the Projection Hibernate class:
critRepeticion.setProjection(Projections.projectionList()
.add( Projections.groupProperty("ensayo") )
.add( CustomProjections.groupByHaving("ensayo_id",Hibernate.LONG,"COUNT(ensayo_id) = "+String.valueOf(lineas.size()))
.add( Projections.rowCount() )
);
The error is:
!STACK 0
java.lang.NullPointerException
at org.hibernate.criterion.ProjectionList.toSqlString(ProjectionList.java:50)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getSelect(CriteriaQueryTranslator.java:310)
at org.hibernate.loader.criteria.CriteriaJoinWalker.<init>(CriteriaJoinWalker.java:71)
at org.hibernate.loader.criteria.CriteriaLoader.<init>(CriteriaLoader.java:67)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1550)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283)
at ar.com.cse.cseagro.controller.RepeticionController.buscarEnsayo(RepeticionController.java:101)
If I comment the line with CustomProjections class, the query work, but I don't get the HAVING COUNT(*) filter in the SQL ...
Basically the query try to retrieve, in a master - detail schema, all the master records where a list of details are simultaneously present, like if you want tho know "which invoices have both products, A and B".
That why if I got 3 items in the IN clause, I need to use HAVING COUNT = 3 clause.
Any idea or suggestion?
Best regards,
I figured out the problem. I replace CusotmProjections class, with:
.add( Projections.sqlGroupProjection("ensayo_id", groupBy , alias, types));
where groupBy, alias and types are:
String groupBy = "ensayo_id" + " having " + "count(*) = " + String.valueOf(lineas.size());
String[] alias = new String[1];
Alias[0] = "ensayo_id";
Type[] types = new Type[1];
types[0] = Hibernate.INTEGER;
and the magic is on groupby String. –
If someone needs to do it in grails it would be like:
projections {
groupProperty("id")
sqlGroupProjection(...)
rowCount()
}
Where sqlGroupProjection is available since 2.2.0
/**
* Adds a sql projection to the criteria
*
* #param sql SQL projecting
* #param groupBy group by clause
* #param columnAliases List of column aliases for the projected values
* #param types List of types for the projected values
*/
protected void sqlGroupProjection(String sql, String groupBy, List<String> columnAliases, List<Type> types) {
projectionList.add(Projections.sqlGroupProjection(sql, groupBy, columnAliases.toArray(new String[columnAliases.size()]), types.toArray(new Type[types.size()])));
}
http://grepcode.com/file/repo1.maven.org/maven2/org.grails/grails-hibernate/2.2.0/grails/orm/HibernateCriteriaBuilder.java/#267
Here is my sample, it works fine, maybe useful :
My sql query :
select COLUMN1, sum(COLUMN2) from MY_TABLE group by
COLUMN1 having sum(COLUMN2) > 1000;
And Criteria would be :
Criteria criteria = getCurrentSession().createCriteria(MyTable.Class);
ProjectionList projectionList = Projections.projectionList();
projectionList.add(Projections.property("column1"), "column1");
projectionList.add(Projections.sqlGroupProjection("sum(column2) sumColumn2 ", "COLUMN1 having sum(COLUMN2) > 1000" , new String[]{"sumColumn2"}, new org.hibernate.type.Type[]{StandardBasicTypes.STRING}));
criteria.setProjection(projectionList);
criteria.List();
criteria.add(Restrictions.sqlRestriction("1=1 having count(*) = 2"));

Inner join query on Hibernate - SQL queries do not currently support iteration

I'm new to hibernate and I've this SQL query which works perfectly
SELECT count(*) as posti_disponibili from occupazione t inner join
(select id_posto_park, max(date_time) as MaxDate from occupazione
group by id_posto_park) tm on t.id_posto_park = tm.id_posto_park and
t.date_time = tm.Maxdate and t.isOccupied = 0
which gives me all the last items with isOccupied = 0
I was porting it into Hibernate, I've tried to use
result = ( (Integer) session.createSQLQuery(query).iterate().next() ).intValue()
to return posti_disponibili but i got this exception
java.lang.UnsupportedOperationException: SQL queries do not currently support iteration
How can i solve this? I cannot find the equivalent HQL query
Thank you
I would suggest you to use
Query#uniqueResult()
which will give you single result.
select count(*) .....
will always return you a single result.
Hibernate support it's own iterator-like scroll:
String sqlQuery = "select a, b, c from someTable";
ScrollableResults scroll = getSession().createSQLQuery(sqlQuery).scroll(ScrollMode.FORWARD_ONLY);
while (scroll.next()) {
Object[] row = scroll.get();
//process row columns
}
scroll.close();

Categories

Resources