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.
Related
In MySQL it works:
SELECT * FROM carparks a
LEFT JOIN (SELECT * FROM locales_carparks)
c ON a.carpark_id=c.carpark_id
Hot to translate it to JPA:
#Query("SELECT a FROM Carparks a LEFT JOIN("
+"SELECT b FROM a.locales b"
+")")
IllegalArgumentException:
org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token:
( near line 1, column 72 [SELECT a FROM
database.model.carpark.Carparks a LEFT JOIN(SELECT b
FROM a.locales b)]
I've simplified example to show the essence of the problem. Normally I use justSELECT a FROM Carparks a LEFT JOIN a.locales and it works, but in my case I want to use nested SELECT because my query is much more complex
You could use a simple alternative
create view v_carparks as
SELECT * FROM carparks a
LEFT JOIN (SELECT * FROM locales_carparks)
c ON a.carpark_id=c.carpark_id
And use it for the query
#Query("SELECT a FROM v_carparks")
Especially if the query is complicated, this would be cleaner to have a huge query in a view to hide that complexity.
EDIT :
You can't used a nested query for join. This is written in the HQL documentation like this :
Note that HQL subqueries can occur only in the select or where clauses.
This could be explain for the mapping system. Hard to do the mapping with a subqueries result.
You can write it like this
#Query("SELECT a FROM Carparks a LEFT JOIN Locales b on a.carpark_id = b.carpark_id")
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();
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.
I am trying to execute an hql query with aliases
select **clbs.id as id**
from ClaimDO cl, ClaimBillSummaryDO clbs, HospitalDO h
where clbs.parentGuidObj.id=cl.id and h.id=cl.hospitalSeq and cl.id= '10721'
and I get the following error
org.hibernate.QueryException: , expected in SELECT
However the query runs without error if i remove the alias
select **clbs.id**
from ClaimDO cl, ClaimBillSummaryDO clbs, HospitalDO h
where clbs.parentGuidObj.id=cl.id and h.id=cl.hospitalSeq and cl.id= '10721'
Why are you not using mappings to join your entities? You might as well use native queries to do this. HQL would look more like the following. I have omitted the HospitalDO join since it doesn't look meaningful.
select clbs.id from ClaimDO cl join cl.parentGuidObj clbs where cl.id = :id
I am trying to get distinct values from table application based on the column entry_id. I've managed to get this working with plain SQL:
SELECT DISTINCT ON (a.entry_id) a.* FROM application AS a
JOIN entry AS e ON e.id = a.entry_id
WHERE e.valid_until BETWEEN ? AND ?;
The problem is, I have to translate it to HQL. Is there any way to resolve it in HQL without Criteria API?
It looks like this feature has been requested a long time ago but is still unresolved.