java - How to use Spring + Hibernate query for join table -
i using spring mvc + hibernate + mysql running on tomcat intellij editor
i want query hql mysql this
select * customer c left join person p on p.idperson = c.idcustomer
it's not working it's return error this
http status 500 - request processing failed; nested exception org.hibernate.hql.internal.ast.querysyntaxexception: path expected join! [from com.springapp.mvc.model.customer c left join person p on p.idperson = c.idcustomer]
here's database
create table `customer` ( `idcustomer` int(11) not null auto_increment, `name` varchar(45) default null, primary key (`idcustomer`), constraint `customer person` foreign key (`idcustomer`) references `person` (`idperson`) on delete no action on update no action ) engine=innodb default charset=utf8; create table `person` ( `idperson` int(11) not null auto_increment, `country` varchar(45) default null, primary key (`idperson`) ) engine=innodb default charset=utf8;
relationship onetoone customer.idcustomer person.idperson
here's code
model.customer.java
@entity @table(name = "customer") public class customer { private int idcustomer; private string name; @id @column(name = "idcustomer") public int getidcustomer() { return idcustomer; } public void setidcustomer(int idcustomer) { this.idcustomer = idcustomer; } @basic @column(name = "name") public string getname() { return name; } public void setname(string name) { this.name = name; } @override public boolean equals(object o) { if (this == o) return true; if (o == null || getclass() != o.getclass()) return false; customer customer = (customer) o; if (idcustomer != customer.idcustomer) return false; if (name != null ? !name.equals(customer.name) : customer.name != null) return false; return true; } @override public int hashcode() { int result = idcustomer; result = 31 * result + (name != null ? name.hashcode() : 0); return result; } }
model.person.java
@entity @table(name = "person") public class person { private int idperson; private string country; @id @column(name = "idperson") public int getidperson() { return idperson; } public void setidperson(int idperson) { this.idperson = idperson; } @basic @column(name = "country") public string getcountry() { return country; } public void setcountry(string country) { this.country = country; } @override public boolean equals(object o) { if (this == o) return true; if (o == null || getclass() != o.getclass()) return false; person person = (person) o; if (idperson != person.idperson) return false; if (country != null ? !country.equals(person.country) : person.country != null) return false; return true; } @override public int hashcode() { int result = idperson; result = 31 * result + (country != null ? country.hashcode() : 0); return result; } }
and implement dao.customerdaoimpl.java
public list<customer> listcustomers() { session session = this.sessionfactory.getcurrentsession(); list<customer> customerslist = session.createquery("from customer c left join person p on p.idperson = c.idcustomer").list(); (customer c : customerslist) { logger.info("customer list::" + c); } return customerslist; }
im new java developer , poor in english please me thank you
hibernate/jpa build kind of database abstraction layer. therefore works entity (instead of tables) , relations (@manytoone...) entities instead of explicite joining ids table columns.
@entity person { @id long id; ... @onetoone @joincolumn(name = "customer_fk") customer customer; //no other customer_fk mapping!!! } @entity customer { @id long id; }
now can select c person p left join p.customer c
Comments
Post a Comment