Discriminator value from separate table in Hibernate inheritance mapping - java

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 "."

Related

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.

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.

Hibernate: column does not exist

When I try to load an Entity with Hibernate I get the following error in postgres-log:
ERROR: column appuser0_.device_token does not exist at character 35
STATEMENT: select appuser0_.id as id1_27_0_, appuser0_.device_token as device_t2_27_0_,....
The column device_token definitely exists - and if I copy-paste the whole logged statement and execute it in PGAdmin, I get the expected result.
So what do I forget? What is the difference between the Hibernate statement and the manually executed one?
This issue was caused by the multi tenant configuration so that the wrong DataSource has been chosen.
Depending on how you defined the query, the problem might be located somewhere else: For example, HQL Queries are using the "property-names" of the class, not the column names.
And if you have something like:
#Column("device_token")
private String deviceToken;
Then your HQL-Query should target "deviceToken" and not "device_token". We also encountered a similar error once: Hibernate was reporting "user_id" is missing, because we named the property "userId" with the underscored version for the column name only.
This might be not the problem for you but worth double checking it.

SQL Firebird implementation in java/ IBSQL

so tried to put that SQL code into my java-aplication:
SELECT DISTINCT
StRzImRo.Rohstoff, StRo.Bezeichnung,
CAST (SUM(BwLsImAt.Lieferungen * StRzImRo.Menge * StAt.PROD__REZEPTURGEWICHT / Coalesce(StRz.PARM__BEZUGSGROESSE,1)) AS NUMERIC (9,3)) Rohstoffverbrauch_Gesamt FROM BwLsImAt
JOIN StAt ON (StAt.IntRowId = BwLsImAt.Artikel)
JOIN StRz ON (StRz.IntRowId = StAt.PROD__REZEPTUR)
JOIN StRzImRo ON (StRzImRo.Master = StRz.IntRowId)
JOIN StRo ON (StRzImRo.Rohstoff = StRo.IntRowId)
WHERE StAt.IntRowId > 0
GROUP BY StRzImRo.Rohstoff, StRo.Bezeichnung
-- GROUP BY StRzImRo.Rohstoff, StRzImRo.Menge, StAt.PROD__REZEPTURGEWICHT, Coalesce(StRz.PARM__BEZUGSGROESSE,1)
The code is fully funcional and tested in IBSQL but not working in my java-application.
My app does work properly with other code. I get this error:
org.firebirdsql.jdbc.FBSQLException: GDS Exception. 335544569. Dynamic SQL Error
SQL error code = -104
Token unknown - line 1, column 266
ON
I would be very happy if someone could help me with this problem. Thanks!
P.S.: Sorry for my bad language, but i´m not a native speaker
The error suggests there is an ON in an unexpected place in your query, and as the query itself looks fine, my guess is the problem is with the way you construct the query in your Java application. There might be some whitespace missing in your query.
My guess is that you have something like
query = "SELECT * " +
"FROM table1" +
"JOIN table2 ON " //.....
The missing whitespace will make the SQL:
SELECT * FROM table1JOIN table2 ON ....
For the parser, this is perfectly valid until it encounters the ON token, which triggers the error. Eg the parser identifies it is a SELECT with * (all) columns from table1JOIN with alias table2. During parsing the server doesn't check if the table actually exists, so it doesn't trip over the fact that table1JOIN doesn't exist. That is checked after parsing is successfully completed.

Execute left join query in JPA

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.

Categories