i have been trying long time make simple query hql return me
java.lang.illegalargumentexception: org.hibernate.hql.internal.ast.querysyntaxexception: unexpected token: on near line 1, column 72 [select p, g.description entity.tb_person p inner join tb_gender g on p.idgender = g.id p.username= :username]
here code query:
string jpql = "select p, g.description " + "from tb_person p inner join tb_gender g " + "on p.idgender = g.id p.username= :username"; query query = manager.createquery(jpql); query.setparameter("username", credentials.getusername()); list l = (list)query.getresultlist();
here entities:
package entity; import java.util.date; import javax.persistence.entity; import javax.persistence.id; import javax.persistence.table; import org.hibernate.annotations.foreignkey; @entity @table(name="tb_person") public class tb_person { @id private string cpf; private string name; private date birthday; private string photopath; @foreignkey(name = "fk_tb_person_tb_user1") private string username; @foreignkey(name = "fk_person_gender") private int idgender; public string getusername() { return username; } public void setusername(string username) { username = username; } public string getcpf() { return cpf; } public void setcpf(string cpf) { cpf = cpf; } public string getname() { return name; } public void setname(string name) { name = name; } public date getbirthday() { return birthday; } public void setbirthday(date birthday) { birthday = birthday; } public string getphotopath() { return photopath; } public void setphotopath(string photopath) { photopath = photopath; } } package entity; import javax.persistence.entity; import javax.persistence.id; import javax.persistence.table; @entity @table(name="tb_gender") public class tb_gender { @id private int id; private string description; private string abbreviation; public string getdescription() { return description; } public void setdescription(string description) { description = description; } public string getabbreviation() { return abbreviation; } public void setabbreviation(string abbreviation) { abbreviation = abbreviation; } public int getid() { return id; } }
what doing wrong?
i think not establishing relationships between entities correctly.
@foreignkey
represents constraint not represent relationship between java entities.
until hibernate 5.1 version, can not use join between entities in jpql if not have established relation between them using annotations @onetoone, @onetomany, @manytoone
, ... more info subject , how join unrelated entities.
to execute query, have define in entities
@entity @table(name="tb_person") public class tb_person { @id private string cpf; @onetoone @joincolumn(name = "user_id") // <- table column constrained fk_tb_person_tb_user1 private tb_user user; ... }