Error generated Hibernate Jpa Repository Query - sql

I use this query with Jpa Repository:
#Query("select f.attrezzature from FoglioLavoro f where f.id = :idFoglioLavoro")
Page<AbstractAttrezzatura> findAttrezzaturaFoglioLavoro(#Param("idFoglioLavoro")Long idFoglioLavoro, Pageable pageable);
But when I use this query I obtain this error:
ERROR: org.hibernate.engine.jdbc.spi.SqlExceptionHelper - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') as col_0_0_ from foglio_lavoro fogliolavo0_, foglio_lavoro_attrezzature attrez' at line 1
And this is the generated spring-data query:
Hibernate:
select
count(.) as col_0_0_
from
foglio_lavoro fogliolavo0_,
foglio_lavoro_attrezzature attrezzatu1_,
abstract_attrezzatura abstractat2_
where
fogliolavo0_.id=attrezzatu1_.fogli_lavoro
and attrezzatu1_.attrezzature=abstractat2_.id
and fogliolavo0_.id=?
How can I solve it??
Thank you very much

When your repository method returns Page<T>, Spring Data JPA tries to infer a query for total count of elements from the query you specified, and sometimes the inferred query is wrong.
In such a case you need to specify a count query explicitly:
#Query(
value = "select f.attrezzature from FoglioLavoro f where f.id = :idFoglioLavoro",
countQuery = "select count(elements(f.attrezzature)) from FoglioLavoro f where f.id = :idFoglioLavoro")
Page<AbstractAttrezzatura> findAttrezzaturaFoglioLavoro(#Param("idFoglioLavoro")Long idFoglioLavoro, Pageable pageable);

Related

Subquery in Update statement not working in Snowflake

I am running below query in Snowflake:
UPDATE PROVIDER_XO_SCORE_TABLE AS PXS
SET PXS.PROVIDER_ID = (SELECT P.PROVIDER_ID
FROM PROVIDER_TABLE P
WHERE PXS.XPI = P.XPI);
This query working fine in MySql but giving below error message in Snowflake.
SQL compilation error:
Unsupported subquery type cannot be evaluated
You can use the join-like syntax with UPDATE...FROM:
UPDATE PROVIDER_XO_SCORE_TABLE PXS
SET PROVIDER_ID = P.PROVIDER_ID
FROM PROVIDER_TABLE P
WHERE PXS.XPI = P.XPI;

How do I access the output of sql in scala?

I'd like to get access to the results from a sql query that is getting sent over to a redshift database in scala.
val test = spark.sql("""
SELECT * FROM my_table
LIMIT 100""")
test.show
When I check the class it appears to be a sql data set.
test.getClass
res52: Class[_ <: org.apache.spark.sql.DataFrame] = class org.apache.spark.sql.Dataset
Unfortunately, it returns this error.
org.apache.spark.sql.catalyst.errors.package$TreeNodeException: execute, tree:
Exchange SinglePartition
How do I show the top results from my sql dataset?

Cast exception in native sql query using hibernate

I have a sql query using tables in it. the result set is return to a bean class which is not mapped to a table in database. the code is here:
SQLQuery q2=ss.createSQLQuery("select tbl_policy.policyNum as POLICYNUM FROM tbl_policy join tbl_product on tbl_policy.FK_productId = tbl_product.pk_product_id join tbl_code on tbl_policy.FK_codeId = tbl_code.PK_codeId join tbl_agriyear on tbl_policy.FK_agriYearId = tbl_agriyear.pk_agriyear_id where tbl_policy.FK_naturalInsurantId = :p1 and tbl_agriyear.AGRIYEAR =:p2");
q2.addScalar("POLICYNUM", Hibernate.STRING);
List<SearchPolicyBean> lsql = (List<SearchPolicyBean>)q2.list();
bean class name is: SearchPolicyBean
when I run it, in this line
System.out.println("Finalllll "+lsql.get(0).getPOLICYNUM());
this error appears:
java.lang.String cannot be cast to BO.SearchPolicyBean
addScalar(String columnAlias)
Is to Declare a scalar query result.
You declared it as a String here q2.addScalar("POLICYNUM", Hibernate.STRING);
So obviously the query results a String list not your SearchPolicyBean pojos .
If you are expectiong an list of your pojo classe you have to use
q2.addEntity(SearchPolicyBean.Class);
And change your query according.
for getting first element:
setPolicyNum((String) ((Object[])polList.get(i))[0]);

Advantage Database 8.1 SQL IN clause

Using Advantage Database Server 8.1 I am having trouble executing a successful query. I am trying to do the following
SELECT * FROM Persons
WHERE LastName IN ('Hansen','Pettersen')
To check for multiple values in a column. But I get an error when I try to execute this query in Advantage.
Edit - Error
poQuery: Error 7200: AQE Error: State = 42000; NativeError = 2115; [iAnywhere Solutions][Advantage SQL Engine]Expected lexical element not found: ( There was a problem parsing the
WHERE clause in your SELECT statement. -- Location of error in the SQL statement is: 46
And here is the SQL i'm executing
select * from "Pat Visit" where
DIAG1 IN = ('43644', '43645', '43770', '43771', '43772', '43773', '43774',
'43842', '43843', '43845', '43846', '43847', '43848', '97804', '98961',
'98962', '99078')
Done
Does anyone have any Idea how I could do something similar in advantage that would be efficient as well?
Thanks
You have an extraneous = in the statement after the IN. It should be:
select * from "Pat Visit" where
DIAG1 IN ('43644', '43645', <snip> )

Java EE not compiling a query with the IN operator

I've written a query that works good if used into MySql Workbench, but it won't compile if used in my Java EE project!
Here's the query :
#NamedQuery(name = "Book.findByCourse", query = "SELECT b FROM Book b WHERE b.teaching IN (SELECT T.id FROM Teaching T, Course C WHERE T.course = C.id AND C.id = :course)")
The query works fine, but I've got this error in my Java EE project :
Error compiling the query [...], line 0, column 0: invalid IN expression argument [SubqueryNode
Left: null
Right: null], expected argument of type [entity.Teaching].
What's wrong with it?
First of all you are trying to select ids only in your subquery and then compare it to full pojo object (not sure if that's possible at all)
In my opinion you should either got with native query (#NamedNativeQuery) to achieve what you want or use Criteria Builder like for example here