QuerySyntaxException : Hibernate not recognizing the postgres query syntax in java - java

I am facing problem of executing the following query in java using hibernate for postgres tables.
The query is made up to retrive the data from 3 tables using Inner Joins.
Query :
QryJourney = "SELECT journey.id , journey.operatingday, journey.linename, journey.scheduledeparture, journey.scheduledeparturestopname, journeydetail.stopname , journeydetail.latitude, journeydetail.longitude FROM journey left join journey_journeydetail ON journey.id = journey_journeydetail.journey_id left JOIN journeydetail ON journey_journeydetail.journeydetails_id = journeydetail.id WHERE journey.id = '155815228' ORDER BY journeydetail.schedulearrival";
as soon as it executes, following exception occured.
Exception :
Exception in thread "main" org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: ON near line 1, column 268 [SELECT journey.id , journey.operatingday, journey.linename, journey.scheduledeparture, journey.scheduledeparturestopname, journeydetail.stopname , journeydetail.latitude, journeydetail.longitude FROM de.db.journeyTracker.model.journey left join journey_journeydetail ON journey.id = journey_journeydetail.journey_id left JOIN journeydetail ON journey_journeydetail.journeydetails_id = journeydetail.id WHERE journey.id = '155815228' ORDER BY journeydetail.schedulearrival]
Tis query works 100% fine at postgres while executing on its SQL Pane.
Anybody having any idea?
Regards
Usman

Hibernate queries are written in Hibernate Query Language (HQL) not in native SQL. Rephrase your query in HQL or use a native query to use SQL with Hibernate.
Hibernate is an object-relational mapper. It won't just give you a result set. If you want that, use JDBC directly, using PgJDBC.
If you want native domain objects as query results, use Hibernate with HQL or via a native query mapping. Native queries are fiddlier becuse you have to explicitly tell Hibernate how all the result columns map to your result objects.

Related

nativeQuery in JPA

I am new to JPA.
I am trying to create a nativequery with 3 joins on 3 tables.
I have written a nativequery which is something like the below:
Query query=entityManager.createNativeQuery("select p.value,m.value,t.value,t.value from ping as p,ming as m,ting as t where p.id=m.vid and m.id=t.vid");
List<Object[]> list = (List<Object[]>) query.getResultList();
I have 3 tables ping,ming,ting in my database.
I have got syntax error during execution.
check the manual that corresponds to your MySQL server version for the right syntax to use near 'ping as p,ming as m ,ting as t';
Would be helpful if some one can point me the error and What would be the better solution to join different tables over nativequery in JPA.
EDIT:I have successfully run the above query on mysql.
'ping as p,ming as m ,ting as t'
Try to put dot instead coma.
Maybe your syntax is incorrect
Some how alias on the column is causing the problem.
I have tried running the program with out alias on the columns and everything is fine.
Found the similar question here.
hibernate native SQL query error

How to rewrite sql statement to work with hibernate?

I'm using PostgreSQL in my java application without ORM. I want to go further and add Hibernate to my project. I have this sql query which I add to PreparedStatement() and it returns a number.
SELECT COUNT(pr.id) FROM prisoner pr
JOIN cell c ON c.id = pr.cell_id
JOIN prison p ON p.id = c.prison_id
WHERE p.id = ?
I'm new to Hibernate. How would you suggest me to rewrite this statement to work with Hibernate? Should I use HSQL, or criteria or query or something different ?
You can do it Either of following way.
1) Keep you query as it and use nativeSQL for hibernate.
hibernate native query, count
2) make model of all your join table and put hibernate query.

JPQL query containing LIKE converted into criteria

I need to use the LIKE operator into an JPA query. I need to use it for types other then String but the JPA criteria API allows me to add only String parameters. I tried using the .as(String.class) but something fails and also tried calling the CAST function from the underlying Oracle that again fails for unknown reasons to me.
I tried writing the query also in JPQL and it works as expected. This is the query:
SELECT p from CustomerOrder p where p.id like '%62%'
UPDATE:
The query must be built in a generic fashion as it is for filtering, so it needs to be created at runtime. On the query that is already created I tried to add the LIKE clause like this:
query.where(builder.like(selectAttributePath.as(String.class), "%"+filterValue.toString().toLowerCase()+"%"));
But this crashes with this exception:
org.hibernate.hql.internal.ast.QuerySyntaxException: expecting CLOSE, found '(' near line 1, column 156 [select distinct generatedAlias0.id from de.brueckner.mms.proddetailschedact.data.CustomerOrder as generatedAlias0 where cast(generatedAlias0.id as varchar2(255 char)) like :param0]
I executed the same query directly to Oracle using SQLDeveloper, so it should be sound from this point of view. So the problem is the Hibernate is the issue. Any suggestions on how to fix it?
How can I write this query using JPA Criteria?
I fixed the problem by invoking the 'TO_CHAR' function from the underlying Oracle DB and using the LIKE operator like for normal String's.
query.where(builder.like(selectAttributePath.as(String.class), "%" +filterValue.toString().toLowerCase() + "%")
You can try the below code, it might require modifications.
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<CustomerOrder> cq = cb.createQuery(CustomerOrder.class);
Root<CustomerOrder> order = cq.from(CustomerOrder.class);
cq.where(cb.like(Long.valueOf(order.get(CustomerOrder_.id)).toString(), "%62%"));
TypedQuery<CustomerOrder> q = em.createQuery(cq);
List<CustomerOrder> results = q.getResultList();

jpa select statement with subnets

I am trying to write query with multiple select subnets in it.But I defined a nativequery
I am giving error. Compiler specifies that "(" after "from" is not proper. How can I define
a native query in JPA 2.0
For eaxmple:
SELECT *
from (SELECT ****C) REI3 where column1 != 1
GROUP BY REI3.column2 order by REI3.column3 ASC
JPA does not have too much to do with validating SQL syntax, query is passed to JDBC driver. Likely you are trying run query such a way, that it is interpreted as JP QL. Instead try following method to execute it as
Query q = em.createNativeQuery("Your SQL here");
Other alternative is to use NamedNativeQuery Example

HQL - get results from select with DOT seperator

Is it possible to get HQL results with dot.
for example:
select Employee.name as 'Employee.name'
I know that mysql allows that, is there any support fir that in hibernate.
This is the exeption that I get:
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.hql.ast.QuerySyntaxException: unexpected token: . near line 1, column 42 [SELECT Affiliate.affiliateId as Affiliate.affiliateId , parent.userName as parent_userName , Affiliate.userName as Affiliate_userName , Affiliate.email as Affiliate_email , parent.affiliateId as parent_affiliateId , employee.firstName as employee_firstName , Affiliate.name as Affiliate_name FROM com.affiliates.hibernate.Affiliate Affiliate INNER JOIN Affiliate.employee as employee INNER JOIN Affiliate.parent as parent WHERE 1=1 AND Affiliate.employee='1']
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:656)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
Because of architecture problems, "_" instad of "." cant work for me.
Thanks
I'm not sure if I understood your question correctly. You are thinking about writing queries. Hibernate supports its internal language called HQL. To be honest I prefer using JPA and hibernate as JPA implementation. In such case you have to write your queries with Criteria Api or using JPA query language. Query language is very convenient in conjunction with NamedQueries. So if you are using HQL or JPA, the easiest way is to select object with some name
SELECT e FROM Employee e WHERE p.name = ?1
The other possibility is using NativeQuery, here is an example.

Categories

Resources