hibernate subquery not work - java

I have to porting sql queries from mysql workbench to Hibernate, but for this query :
Query query5_1 = em.createQuery("select result.id,result.name,result.surname,max(result.sales)
from (select m.warehousemen.id as id,m.warehousemen.name as name,m.warehousemen.surname as
surname,sum(m.size) as sales from MovementsEntity m group by m.warehousemen.id) result");
i get this error:
error log
please help me, thanks anyway

You can't do such things with HQL, you should use native query for this.

Related

no result after transforming postgres query to jpql in spring data jpa

I am trying to write a query to fetch list as this query is native sql query, all I need is to transform to spring jpql in which I am failing badly. if there is any link related to this please let me know
I am supposed to get list from this query. as this query is working fine with postgres console but when I even tried this with spring jpa as native query
it is showing results in console but not fetching in service layer [edit:] I mean not calculating any result set.
I am sure I am missing some important/small thing here.
below is the native postgres query
selcet t.id,count(*), count(*) filter (where t.status = 'DONE') from table t where t.id in ([list]) group by t.id
what Im trying is
SELECT t.id, count(t), count(t.id) where staus = 'DONE' from Table t where t.id in ([list]) group by t.id
edit: with constructor based query this is not even working
I am not even sure how to start this query
while being new to this I am not even able to start how to solve this.
Any hint, insight will be useful

How to convert this query to JPA Query

SELECT *
FROM m_country_mk_py p,m_country_mk_sy s
where p.id = s.mapping_id and s.lang_cd="en";
I'm not able to write same query in JPA, I'm facing some syntactical issue.

How to use JPA Prepared statment for Insert query in java

I would like to know how to use prepared statement for insert query. Generally for select query I am using in following way.
Query query = JPA.em()
.createNativeQuery("select item_status from item_details where box_id=:boxnumber");
query.setParameter("boxnumber", boxNumber);
But when I am using insert query I am unable to use in the above way.
Query query = JPA.em()
.createNativeQuery("insert into item_details values(':item_status')");
query.setParameter("item_status", itemstat);
I am getting error like
java.lang.IllegalArgumentException:
org.hibernate.QueryParameterException: could not locate named parameter [item_status]
at org.hibernate.ejb.QueryImpl.setParameter(QueryImpl.java:368) ~[hibernate-entitymanager-3.6.9.Final.jar:3.6.9.Final]
at org.hibernate.ejb.QueryImpl.setParameter(QueryImpl.java:72) ~[hibernate-entitymanager-3.6.9.Final.jar:3.6.9.Final]
please any one help me to sort out this issue. Thanks in advance
I could't tested but could you try this;
Query query = JPA.em().createNativeQuery("insert into item_details(item_status) values(?)") .setParameter(1, itemstat).executeUpdate();

How to use CONCAT_WS() in Hibernate HQL

To combine multiple columns as one,
I found one answer
SELECT id,CONCAT_WS(',', field_1, field_2, field_3, field_4) list
FROM `table`;
This query working fine in SQL but it gives me error in HQL:
Error is .
(java.lang.IllegalStateException: No data type for node: org.hibernate.hql.internal.ast.tree.MethodNode )
please help me to find out what wrong i did, help me to know how to use CONCAT_WS() IN HQL
below how i written my HQL query
SELECT C1._URI,C1.HEALTH_FACILITY,C1.DISTRICT,CONCAT_WS(',', C1.BLOCKS_OF_BHUBRI, C1.BLOCKS_OF_GOLAGHAT, C1.BLOCKS_OF_HAILAKANDI) as Block_name
FROM GapAnalysisWashInHealthFacilitiesCore C1
any help will appreciate
CONCAT_WS is a function specific to mySql. HQL is a generic language and not aware of native SQL functions and syntax. If you really need the function, then you should use Hibernate's API for native SQL.
Session session = ...;
Query query = session.createSQLQuery("
SELECT id,CONCAT_WS(',', field_1, field_2, field_3, field_4) Block_name FROM `table`");
List result = query.list();
Then you may like to have a look at Result Transformers to get result as list of GapAnalysisWashInHealthFacilitiesCore objects.

Hibernate session.createQuery error while replace method with single quote

I got very typical issue. My dynamically generated query like this...
UPDATE Templates t SET t.TEMPLATE_DATA = replace(t.TEMPLATE_DATA, 'Test\'s Test', 'Kent"s Test'), t.TEMPLATE_DATA = replace(t.TEMPLATE_DATA, 'Test"s Test', 'Kent"s Test'), UPDATE_DATE = NOW() where PRACTICE_ID = 1 AND CATEGORY_ID IN (1)
This works perfect when I explictily fire this query in db. but by using hibernate's session.createQuery(-- my query --) if thwows an error QueryTranslatorException.
Database : Mysql 5.3
Have any one faced this issue?
Thanks in advance.
Try to run this in Hibernate as native SQL query:
session.createSQLQuery(-- query text --);
Because if you use
session.createQuery(-- query text --);
Hibernate will try to execute it as HQL query which differs from usual SQL query.
HQL is object oriented query language. It operates in terms of objects rather then in terms of tables. Here posted a brief description of difference between SQL and HQL. But if you have time better to read appropriate sections of hibernate's documentation about HQL and Native SQL usage.
If you want to execute SQL Query in hibernate, Use : session.createSQLQuery(String query);

Categories