Execute left join query in JPA - java

I have two tables, one for party and one for scorecard template mapping. The scorecard template mapping table has a foreign key back to the party (on id). I want to find a list of all of the parties that have scorecard template mapping details.
But I get an error which says :
java.lang.IllegalArgumentException:
org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token:
on near line 1, column 172 [select new
ScorecardTemplateMapping(p,temMap.scoTemplate,temMap.wrkFlwTemplate)
from com.kpisoft.common.web.domain.Party p left outer join
ScorecardTemplateMapping temMap on temMap.organization.id=p.id and
temMap.gradeType.id=:gradeType where
p.organization.organizationTypeId=:orgType and p.clientId=:clientId
order by p.organization.name]
This is my query:
Query q = entityManager.createQuery("select new
ScorecardTemplateMapping(p,temMap.scoTemplate,temMap.wrkFlwTemplate)
from Party p left outer join ScorecardTemplateMapping temMap on
temMap.organization.id=p.id and temMap.gradeType.id=:gradeType where
p.organization.organizationTypeId=:orgType and p.clientId=:clientId
order by p.organization.name");
I have no idea why this isn't working. Please help!

Error message about syntax error is quite clear:
unexpected token: on
There is no support to make join with ON [conditional] in JPQL (ON is not reserved word). How joins are made in JPQL, is told for example here. It boils down to that you have to present join condition in where clause.

Related

#Query for joining two tables

I am trying to join two entities in a third one using #Query method.
#Query("SELECT new com.concretepage.entity.DeptEmpDto(d.departmentId,d.departmentName,d.managerId,d.locationId,e.employeeId,e.firstName,e.lastName,e.phoneNumber,e.hireDate,e.jobId,e.salary,e.commissionPct) FROM Employee e INNER JOIN Department d")
List <DeptEmpDto> fetchEmpDeptDataInnerJoin();
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 1.
I cannot understand where is my mistake.Any help will be appreciated :).
You missed the joining condition after joining tables using ON clause. So just change your query with this:
#Query("SELECT new com.concretepage.entity.DeptEmpDto(d.departmentId,d.departmentName,d.managerId,d.locationId,e.employeeId,e.firstName,e.lastName,e.phoneNumber,e.hireDate,e.jobId,e.salary,e.commissionPct) FROM Employee e INNER JOIN Department d on e.joining_column_from_table1=d.joining_column_from_table2")
Make sure to replace joining_column_from_table1 and
joining_column_from_table2 with your column names from table
Employee and Department respectively

Error in Update JPA Query

I have a problem with this query when run is as follows
Query query = em.createQuery("UPDATE Equipo c JOIN c.histAsociados e SET e.horasTrabajadas = (CAST(c.horastd AS DECIMAL(18,2)) - (c.horastotales AS DECIMAL(18,2))) WHERE c.id=" + equipo.getId());
This is the exception that gets thrown when running
The SET identifier is missing from the UPDATE clause. [39, 39] The equal sign must be specified.[16, 38] The expression is invalid, which means it does not follow the JPQL grammar.[40, 43] The identification variable 'SET' cannot be a reserved word.[115, 115] The right parenthesis is missing from the sub-expression.[115, 115] The right parenthesis is missing from the sub-expression.[116, 126] The expression is invalid, which means it does not follow the JPQL grammar.[133, 151] The query contains a malformed ending.
Try something like this, putting it into a subquery, assuming that histAsociados is an entity inside Equipo entity:
Query query = em.createQuery("UPDATE Equipo c SET c.histAsociados WHERE c.histAsociados.id in(select Equipo.id from Equipo c1 LEFT JOIN c1.histAsociados e WHERE e.horasTrabajadas = (CAST(c1.horastd AS DECIMAL(18,2)) - (c1.horastotales AS DECIMAL(18,2))) AND c1.histAsociados.id = e.id) AND c.id=:id).setParameter("id", equipo.getId())
Also, could you please try putting some simple names to the attributes of entities. That increases readability.
According to the JPA specification, your syntax cannot be correct. The simplified form of the syntax for the UPDATEstatement can be stated as follows:
UPDATE <entity_name> <identification_variable> SET <identification_variable>.<state_field> = <value> WHERE <condition>
Applying this for your case it might look like:
"UPDATE HistAsociado?? e SET e.horasTrabajadas = (SELECT (CAST(c.horastd AS DECIMAL(18,2)) - CAST(c.horastotales AS DECIMAL(18,2))) AS DIFF FROM Equipo c WHERE c.id= " + equipo.getId())
In the above statement, I was guessing that HistAsociado could be an Entity name; otherwise you have to correct it!
The result of the subquery must also be a single value.
Warning: The modified statement would update all the records in the table as there is no WHERE condition is specified.
So, use this as a hint to solve the problem and don't use it before you have corrected the guessing and added the WHERE condition.
For more information and examples, you could read JPA update statement and this one too.

how to use left outer join in hibernate using hibernate query language

hi i am trying to execute the hql query using left outer join it is thowing exception as org.hibernate.hql.ast.QuerySyntaxException: unexpected token: on near line 1, can pls tell me what is worng in this query
select * from CreditCardDetails cred left outer join CustomerHistory custHist on cred.creditCardDetailsId=custHist.creditCardDetailsId and custHist.cardA=0000
Assuming that you have an association named history that relates the entity CreditCardDetails to CustomerHistory.
from CreditCardDetails cred
left outer join cred.history custHist
with custHist.cardA=0000

SQL generated by Hibernate invalid for MS SQL

Hibernate (HQL) generated the following SQL for which I inserted the parameters:
select
sum(activity0_.calculated_work) as col_0_0_
, employee2_.id as col_1_0_
, projectele1_.id as col_2_0_
from
activity activity0_
inner join generic_object activity0_1_ on activity0_.id=activity0_1_.id
left outer join project_element projectele1_ on activity0_.project_element_id=projectele1_.id
left outer join employee employee2_ on activity0_.owner_id=employee2_.id
left outer join org_unit orgunit3_ on employee2_.org_unit_id=orgunit3_.id
where
activity0_1_.deleted=0 and
activity0_.client_id=22
group by
employee2_.id order by SUM(activity0_.calculated_work) DESC
Error message: Column 'project_element.id' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
I executed this SQL directly in the SQL Server Studio with the same result. I commented this line:
, projectele1_.id as col_2_0_
The SQL was then accepted by the SQL Server
The table project_element definitely has a column with the name id it is also referenced in the LEFT OUTER JOIN and there this column is not causing an error.
Removing the alias projectele1_ had no effect.
To me this looks like a really simple SQL statement. I cannot imagine what is wrong with it and the error message is not helping at all. Any ideas what could be wrong with the SQL?
Your SQL syntax is wrong.If you add projectele1_.id to group by clause it will work.Only aggregate functions work in select statement with group by clause.Or if you remove projectele1_.id from select it will work fine.
My mistake. I should have read the error message several times. projectele1_id is not in the group by clause. MS SQL does not allow to include such a column into the select list. This seems to be a consistency check.
Too bad though that the usage of HQL leads to such an exception in SQL Server but not in MySQL Server.

Discriminator value from separate table in Hibernate inheritance mapping

I have two tables, price_feed and price_feed_type. Price feed will contain the data for different implementations of price feeds.
Now, this would usually be done through the <discriminator> tag, with a discriminator field in the price_feed table. However, it would be preferable in the context of the system to have this field in the price_feed_type table. I've come across the <discriminator formula="..."/> method, and tried working with this.
One solution is as follows (assume this is entered in the formula attribute):
(SELECT implementing_class FROM price_feed_type INNER JOIN price_feed ON price_feed.price_feed_type_id = price_feed_type.price_feed_type_id")
Another solution is the following, and I should note that I've tried with both priceFeedID and price_feed.price_feed_type_id
(SELECT implementing_class FROM price_feed_type WHERE price_feed_type.price_feed_type_id = price_feed.price_feed_type)
Either one gives the following error:
org.postgresql.util.PSQLException: ERROR: column timedprice3_.implementing_class does not exist
An extra note, without the parantheses, I get the error:
org.postgresql.util.PSQLException: ERROR: syntax error at or near "SELECT"
Any ideas as to how to fix this?
EDIT:
To add what I've found so far:
Using either one, if I do SELECT *, and then do SELECT implementing_class outside, it gives a different error. Full code:
(SELECT implementing_class FROM (SELECT * FROM price_feed_type INNER JOIN price_feed ON price_feed.price_feed_type_id = price_feed_type.price_feed_type_id) AS foo)
The error is:
org.postgresql.util.PSQLException: ERROR: syntax error at or near "."

Categories