org.hibernate.exception.SQLGrammarException: could not execute query using scroll - java

I have the following query,
When I tried to execute it using hibernate I get the following exception :
org.hibernate.exception.SQLGrammarException: could not execute query
using scroll.
In my code I have scroll has FORWARD_ONLY.
Can anyone please provide a perfect solution.
select * from (SELECT account_no AS accountno,rownum r FROM sc_dcm_postpaid_index WHERE groupid = 'SBG-2012'
and concat(trim(TO_CHAR(bill_date,'MONTH')),concat('-',TO_CHAR(bill_date,'YY')))='JUNE-12'
AND ROWID IN (SELECT MAX(ROWID) AS row_no ; FROM sc_dcm_postpaid_index
WHERE groupid= 'SBG-2012' and concat(trim(TO_CHAR(bill_date,'MONTH')),concat('-',TO_CHAR(bill_date,'YY')))='JUNE-12'
GROUP BY account_no HAVING COUNT (account_no) >= 1 ) ORDER BY account_no)where r >= 11 and r <= 21..
Also when I change the query to
SELECT account_no AS accountno,rownum r FROM sc_dcm_postpaid_index WHERE rownum >= 11 and rownum <= 21 groupid = 'SBG-2012'
and concat(trim(TO_CHAR(bill_date,'MONTH')),concat('-',TO_CHAR(bill_date,'YY')))='JUNE-12'
AND ROWID IN (SELECT MAX(ROWID) AS row_no ; FROM sc_dcm_postpaid_index
WHERE groupid= 'SBG-2012' and concat(trim(TO_CHAR(bill_date,'MONTH')),concat('-',TO_CHAR(bill_date,'YY')))='JUNE-12'
GROUP BY account_no HAVING COUNT (account_no) >= 1 ) ORDER BY account_no
And this is my Query:
SQLQuery mainQuery = (SQLQuery) session.createSQLQuery(strReportQuery);
ScrollableResults itrDataList = mainQuery.scroll(ScrollMode.FORWARD_ONLY);
Here strReportQuery is my query
It executes fine but it works only from rownum 1 to 10.
From next rownum it gives resultset as empty.
Thanks in Advance,
Bhargavi T.

Related

JPA select from select distinct

I'm trying to execute the following SQL query writing native query in JPA:
SELECT id FROM (
SELECT DISTINCT id, DENSE_RANK() OVER (ORDER BY id) AS row FROM table
) WHERE row BETWEEN 1 AND 10;
In the Query annotation I wrote:
#Query(value = "SELECT t.id FROM (SELECT DISTINCT(e.id), DENSE_RANK() OVER (ORDER BY e.id) AS row FROM Table e) t WHERE row BETWEEN ? AND ?")
but I have syntax errors here FROM (SELECT.
Please, can you help me?
You have probably forgot nativeQuery=true
Try this:
#Query(value = "SELECT t.id FROM (SELECT DISTINCT(e.id), DENSE_RANK() OVER (ORDER BY e.id) AS row FROM Table e) t WHERE row BETWEEN ? AND ?", nativeQuery = true)

java.sql.SQLException: Invalid use of group function

From this query:
select count(*) as data, LOCATION_Name as location
from SPECIAL_PROCEDURE_COLLECTION_FINAL_temp_final
where sno>=6973
and sno<= 7251
and date(PAYMENT_DATE) = '2021-07-31'
and Procedures_Category IN ('FFA-VR')
and SUM(BILLED_AMOUNT)= 0
group by LOCATION_Name
I am getting this error:
java.sql.SQLException: Invalid use of group function
What is the issue?
You cannot use an aggregate function in the where clause. Kindly change yor query to:
select count(*) as data, LOCATION_Name as location
from SPECIAL_PROCEDURE_COLLECTION_FINAL_temp_final
where sno>=6973 and sno<= 7251 and
date(PAYMENT_DATE) = '2021-07-31' and Procedures_Category IN ('FFA-VR')
group by LOCATION_Name
Having SUM(BILLED_AMOUNT)= 0
The restriction on the sum of the billed amount belongs in the HAVING clause, rather than the WHERE clause:
SELECT COUNT(*) AS data, LOCATION_Name AS location
FROM SPECIAL_PROCEDURE_COLLECTION_FINAL_temp_final
WHERE sno BETWEEN 6973 AND 7251 AND
PAYMENT_DATE >= '2021-07-31' AND PAYMENT_DATE < '2021-08-01' AND
Procedures_Category = 'FFA-VR'
GROUP BY LOCATION_Name
HAVING SUM(BILLED_AMOUNT) = 0;

Hibernate: Pagination with setFirstResult and setMaxResult

I'm working on a Java EE project that uses Hibernate as ORM framework.
In order to paginate the results of queries, I'm using the .setFirstResult and .setMaxResult methods (Criteria API).
The problem is that the first page is displayed correctly but when I go to page 2, I have the first result displayed equal as the last result of page one.
By switching the logging level to debug I've managed to catch the SQL query that Hibernate builds. They are:
-- First page query (results from 1 to 10)
select * from ( select this_.DT_FINE_VAL as DT1_5_0_, this_.DT_INI_VAL as DT2_5_0_, this_.CD_TIPO_PERIODO as CD3_5_0_, this_.DT_AGGIORNAMENTO as DT4_5_0_, this_.DT_INSERIMENTO as DT5_5_0_, this_.CD_USERID_AGG as CD6_5_0_, this_.CD_USERID_INS as CD7_5_0_ from GPER0_POVS2.T_POVS2_PERIODI_FUNZ this_ order by this_.CD_TIPO_PERIODO desc ) where rownum <= 10;
-- Second page query (results from 11 to 20)
select * from ( select row_.*, rownum rownum_ from ( select this_.DT_FINE_VAL as DT1_5_0_, this_.DT_INI_VAL as DT2_5_0_, this_.CD_TIPO_PERIODO as CD3_5_0_, this_.DT_AGGIORNAMENTO as DT4_5_0_, this_.DT_INSERIMENTO as DT5_5_0_, this_.CD_USERID_AGG as CD6_5_0_, this_.CD_USERID_INS as CD7_5_0_ from GPER0_POVS2.T_POVS2_PERIODI_FUNZ this_ order by this_.CD_TIPO_PERIODO desc ) row_ where rownum <= 20) where rownum_ > 10;
It seems that the second query is "wrong".
I'm using Oracle as DBMS.
Could this be an Hibernate bug? Can someone help me?
Thanks.
EDIT:
This is the code:
Session currentSession = getCurrentSession();
Criteria criteria = currentSession.createCriteria(PeriodoFunz.class);
criteria.setResultTransformer(Criteria.ROOT_ENTITY);
Order order = paginationInfo.isAsc() ? Order.asc(paginationInfo.getOrderBy()) : Order.desc(paginationInfo.getOrderBy());
criteria.addOrder(order);
....
criteria = criteria.setFirstResult(paginationInfo.getFromRecord()).setMaxResults(paginationInfo.getPageSize());
List<PeriodoFunz> result = criteria.list();
It seems that your order criteria leads to a SQL query that is not stable (returns the same result rows in different order for the queries).
You can circumvent this by adding a second order criteria for a unique attribute, e.g. the ID:
Order order = paginationInfo.isAsc() ? Order.asc(paginationInfo.getOrderBy()) : Order.desc(paginationInfo.getOrderBy());
criteria.addOrder(order);
Order orderById = paginationInfo.isAsc() ? Order.asc("id") : Order.desc("id");
criteria.addOrder(orderById);

SQL Inner Queries- Subquery returns more than 1 row

SELECT DISTINCT cmt.topic_id,ctt.description AS topic_name,cmrt.room_id,cmrt.family_id, crt.title AS room_name, ft.family_identifier AS family_name, upt.gender, cmt.postedby_userid, cmt.member_id,
ut.picture_filename AS senderImage,
ut.croppedpicture_filename AS senderCroppedImage,
cmt.image AS imageUrl ,
cmt.message AS caption,
cmrt.user_id,
(SELECT COUNT(id) FROM `conversation_messages_tbl` a ,`conversation_msgreadstatus_tbl` b WHERE a.`message_id` = b.`message_id`
AND a.`topic_id` = b.`topic_id` AND b.`is_read`= 0 AND b.`user_id`!= 27 GROUP BY b.`user_id`) countn
FROM conversation_messages_tbl cmt,
conversation_topics_tbl ctt ,
conversation_msgreadstatus_tbl cmrt,
conversation_rooms_tbl crt, family_tbl ft,
user_profileinformation_tbl upt,
user_tbl ut
WHERE ctt.topic_id=cmt.topic_id AND cmrt.message_id=cmt.message_id
AND upt.user_id=cmt.postedby_userid AND crt.room_id=cmrt.room_id AND ft.family_id=crt.family_id
AND ut.user_id=cmt.postedby_userid AND cmt.message_id=202 GROUP BY cmrt.user_id;
I'm getting the error message saying
Error Code: 1242
Subquery returns more than 1 row
Solutions ??
You sub query will return multiple rows, because you have used GROUP BY b.user_id and there is not condition between your query and sub query.
If I am thinking in correct way, cmrt.user_id is equals to b.user_id, you can add a condition to your sub query like the following:
(SELECT COUNT(id) FROM `conversation_messages_tbl` a
`conversation_msgreadstatus_tbl` b WHERE a.`message_id` = b.`message_id`
AND a.`topic_id` = b.`topic_id` AND b.`is_read`= 0 AND b.`user_id`!= 27
b.`user_id`=cmrt.user_id) countn
Your inner query
(SELECT COUNT(id) FROM `conversation_messages_tbl` a ,`conversation_msgreadstatus_tbl` b WHERE a.`message_id` = b.`message_id` AND a.`topic_id` = b.`topic_id` AND b.`is_read`= 0 AND b.`user_id`!= 27 GROUP BY b.`user_id`) countn
may return more than 1 row, as it will return a count for each distinct user_id different from 27 (because of group by).

Can I add amounts from multiple tables in hql

I would like to perform a query in HQL similar to this in SQL:
SELECT (
SELECT COUNT(*) FROM EMPLOYER +
SELECT COUNT(*) FROM COMPANIES
)
FROM DUAL
When I add "FROM DUAL" to the query I get an error:
org.hibernate.hql.ast.QuerySyntaxException: DUAL is not mapped
And if I leave off the "FROM DUAL" I get:
org.hibernate.hql.ast.QuerySyntaxException: unexpected end of subtree
Any recommendations?
In SQL, subqueries need their own set of parentheses, so try this:
SELECT ((SELECT COUNT(*) FROM EMPLOYER) +
(SELECT COUNT(*) FROM COMPANIES)
)
If that doesn't work, resort to a from clause:
SELECT e.cnt + c.cnt
FROM (SELECT COUNT(*) as cnt FROM EMPLOYER
) e CROSS JOIN +
(SELECT COUNT(*) as cnt
FROM COMPANIES
) c
Since you didn't join the Company and Employee tables you'd better run two queries:
int companyCount = ((Number) getSession().createQuery("select count(*) from Company").uniqueResult()).intValue();
int employeeCount = ((Number) getSession().createQuery("select count(*) from Employee").uniqueResult()).intValue();
int totalCount = companyCount + employeeCount;
The (Number) is needed since PostgreSQL returns a BigInteger while HDSQLDB returns an Integer, but both are subclasses of Number.

Categories