I'm using Hibernate through Spring Data JPA and trying to add a calculated field. A simple SELECT 1*1
query works, but when I add the real formula, Hibernate gets totally confused and generates a syntactically invalid query.
Parent table:
@Entity
@Table(name = "szallitolevel")
public class Szallitolevel {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
@Min(1)
private Long szam;
@ManyToOne(cascade = CascadeType.REFRESH, fetch = FetchType.EAGER)
@Fetch(FetchMode.JOIN)
private Partner partner;
@Formula("(select sum(xx.mennyiseg) from szallitolevel_sor xx where xx.szallitolevel = id)")
// @Formula("(select 1*1)")
private Long sumMennyiseg;
@OneToMany(mappedBy="szallitolevel", cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval=true)
@Fetch(FetchMode.SUBSELECT)
@Valid
private List<SzallitolevelSor> sorok = new AutoPopulatingList<SzallitolevelSor>(SzallitolevelSor.class);
}
Child table:
@Entity
@Table(name = "szallitolevel_sor")
public class SzallitolevelSor {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private String nev;
@Min(0)
private Long mennyiseg;
@ManyToOne
private Szallitolevel szallitolevel;
}
There is not even a sum() in the generated query, and the formula somehow found its way to the WHERE part:
select
sorok0_.szallitolevel as szallito4_2_1_,
sorok0_.id as id1_3_1_,
sorok0_.id as id1_3_0_,
sorok0_.mennyiseg as mennyise2_3_0_,
sorok0_.nev as nev3_3_0_,
sorok0_.szallitolevel as szallito4_3_0_
from szallitolevel_sor sorok0_
where sorok0_.szallitolevel
in (select szallitole0_.id
from szallitolevel_sor xx
where xx.szallitolevel = szallitole0_.id) as formula0_0_,
partner1_.nev as nev2_1_1_,
partner1_.penz as penz3_1_1_
from szallitolevel szallitole0_
left outer join partner partner1_
on szallitole0_.partner=partner1_.id)
{FAILED after 0 msec}
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'as formula0_0_, partner1_.nev as nev2_1_1_, partner1_.penz as penz3_1_1_ from sz' at line 1
UPDATE: I'm using JDBCTemplate for the relevant query, see answer