I'm developing my first project with JPA, with MySQL as my database and Hibernate 4.3.8 as my JPA provider in a Spring 4 web project.
In my Spring config, I set the database and the dialect:
HibernateJpaVendorAdapter hjpaVA = new HibernateJpaVendorAdapter();
hjpaVA.setDatabase(Database.MYSQL);
hjpaVA.setDatabasePlatform("org.hibernate.dialect.MySQL5Dialect");
I try to get this query that gives me trouble:
TypedQuery<KundeDTO> query = entityManager.createQuery("select new zdb.dto.KundeDTO(k.id, k.firma.firmenname, k.regnr, k.kategorie)
from Kunde k where k.id = :id", KundeDTO.class);
This is the SQL that Hibernate generates:
select kunde0_.`id` as col_0_0_, firma1_.`firmenname` as col_1_0_, kunde0_.`regnr` as col_2_0_, kunde0_.id_kategorie as col_3_0_
from `zdb_e`.`Kunde` kunde0_, `zdb_e`.`Firma` firma1_
inner join `zdb_e`.`Kategorie` kategorie2_ on kunde0_.id_kategorie=kategorie2_.`id`
where kunde0_.id_firma=firma1_.`id` and kunde0_.`id`=1;
Note that there are no parentheses on the inner join!
Running this statement results in the following error:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:
Unknown column 'kunde0_.id_kategorie' in 'on clause'
The reason for the exception is detailed here: mysql-unknown-column-in-on-clause
When I add the parentheses to the where and the inner join clauses and run the statement directly against the database it works:
select kunde0_.`id` as col_0_0_, firma1_.`firmenname` as col_1_0_, kunde0_.`regnr` as col_2_0_, kunde0_.id_kategorie as col_3_0_
from (`zdb_e`.`Kunde` kunde0_, `zdb_e`.`Firma` firma1_ )
inner join `zdb_e`.`Kategorie` kategorie2_ on
( kunde0_.id_kategorie=kategorie2_.`id` )
where kunde0_.id_firma=firma1_.`id` and kunde0_.`id`=1;
So, how can I persuade Hibernate to generate the query like that?
update: here are the entities
Kunde
@Entity
public class Kunde implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
private Integer id;
private Integer regnr;
@OneToOne(optional=false)
@JoinColumn(name="id_firma", nullable = false)
private Firma firma;
@OneToOne(optional=false)
@JoinColumn(name="id_kategorie", nullable = false)
private Kategorie kategorie;
@OneToOne(optional=false)
@JoinColumn(name="id_lieferregion", nullable = false)
private Lieferregion lieferregion;
// getters and setters....
}
Firma
@Entity
@Table(name = "Firma")
public class Firma implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
private Integer id;
@Column(name="firmenname")
private String firmenname;
@Column(name="uid")
private String uid;
@OneToOne(optional=false)
@JoinColumn(name="id_anschrift", nullable = false)
private Anschrift anschrift;
@OneToMany(mappedBy="id_firma", fetch=FetchType.EAGER)
private List<Person> personen;
public Firma() {
personen = new ArrayList<Person>();
}
// getters and setters....
}
Kategorie
@Entity
public class Kategorie implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
private Integer id;
private Integer nummer;
private String bezeichnung;
public Kategorie() {
}
public Kategorie(int kategorieId, int kategorieNummer, String kategorieBezeichnung) {
this.id = kategorieId;
this.nummer = kategorieNummer;
this.bezeichnung = kategorieBezeichnung;
}
// getters and setters....
}
DB schemas
CREATE TABLE kategorie
(id INTEGER NOT NULL AUTO_INCREMENT,
nummer INTEGER NOT NULL,
bezeichnung VARCHAR(100) NOT NULL,
PRIMARY KEY (id),
UNIQUE (nummer, bezeichnung)
);
CREATE TABLE firma
(ID INTEGER NOT NULL AUTO_INCREMENT,
firmenname VARCHAR(50) NOT NULL,
uid VARCHAR(20) NOT NULL,
url VARCHAR(100) NOT NULL,
id_anschrift INTEGER NOT NULL,
id_logo INTEGER,
PRIMARY KEY (id),
UNIQUE (uid),
UNIQUE (firmenname),
UNIQUE (id_anschrift),
CONSTRAINT firma_fk1 FOREIGN KEY (id_anschrift) REFERENCES ANSCHRIFT (ID),
CONSTRAINT firma_fk2 FOREIGN KEY (id_logo) REFERENCES logo (ID));
CREATE TABLE kunde
(id INTEGER NOT NULL AUTO_INCREMENT,
regnr INTEGER NOT NULL,
id_kategorie INTEGER NOT NULL,
id_firma INTEGER NOT NULL,
id_benutzer INTEGER,
id_lieferregion INTEGER NOT NULL,
PRIMARY KEY (id),
UNIQUE (regnr, id_kategorie),
UNIQUE (id_firma),
UNIQUE (id_benutzer),
CONSTRAINT kunde_fk1 FOREIGN KEY (id_firma) REFERENCES firma (id),
CONSTRAINT kunde_fk2 FOREIGN KEY (id_benutzer) REFERENCES benutzer (id),
CONSTRAINT kunde_fk3 FOREIGN KEY (id_kategorie) REFERENCES kategorie (id),
CONSTRAINT kunde_fk4 FOREIGN KEY (id_lieferregion) REFERENCES lieferregion (id)
);
upon further testing
The problem are the missing parentheses on the from clause.
Going directly against the db:
select k.id, k.regnr, f.firmenname from (Kunde k, Firma f) JOIN kategorie kat on k.id_kategorie = kat.id where k.id = 1 and k.id_firma = f.id;
works!
select k.id, k.regnr, f.firmenname from Kunde k, Firma f JOIN kategorie kat on k.id_kategorie = kat.id where k.id = 1 and k.id_firma = f.id;
Doesn't work: Unknown column 'k.id_kategorie' in 'on clause'
Why do I even need the parentheses on the from clause?
And how can I make Hibernate put them in?