I am using Java 8 with JPA/Hibernate 5.2.1 and a MySQL database.
Tables:
+--------+ +----------------+ +----------+
| Rating | | Rating_Employee| | Employee |
+--------+ +----------------+ +----------+
| ID | | RAT_ID | | ID |
| REVIEW | | EMP_ID | +----------+
+--------+ +----------------+
I am trying to save a row in a Rating
table, but get the following:
MySQLSyntaxErrorException: Unknown column 'employee0_.distance' in 'field list'
The reason is because the Employee model object has a DISTANCE
field, but no matching column on the database. I need the DISTANCE
field because when I do a SELECT
on the Employee table, I calculate the DISTANCE
based on other columns with a nativeQuery
. However, when Hibernate does the select itself when updating the joined table RATING
, it wants to match DISTANCE
, which does not exist (I get the above error).
When I persist
or merge
the Employee object, I use the following successfully:
@Column(insertable=false, updatable=false)
private BigDecimal distance;
Question:
How do I get Hibernate to ignore the DISTANCE
when doing a non native query select
? (When it updates RATING
table)
UPDATE
I was advised to possibly use @Formula("...")
, but my method has a pretty complex nativeQuery
, which also order by distance
. If I put the formula into @Formula("...")
, it needs to also receive parameters currently passed to the method.
Any ideas?