java - Simple @ManyToOne relation: could not resolve property (org.hibernate.QueryException) -


i have simple relationship - can seen in picture:

enter image description here

as try read read out city country_id:

country austria = (country) session.load(country.class, 1l);  // works expected system.out.println(austria.tostring());          criteria crcities = session.createcriteria(city.class); crcities.add(restrictions.eq("country_id", austria.getid())); // crashes .. list<city> cities = crcities.list();  system.out.println("show cities of austria:"); (city next : cities) {     system.out.println(" - " + next.getname()); }   

i'm getting following error:

exception in thread "main" org.hibernate.queryexception: not resolve property: country_id of: com.mahlzeit.datamodel.geographic.city

this pojos:

country.java

package com.mahlzeit.datamodel.geographic;  import javax.persistence.entity; import javax.persistence.generatedvalue; import javax.persistence.id; import javax.persistence.table;  @entity @table public class country {      @id     @generatedvalue     private long id;      private string country_code;      public string getcountrycode() {         return country_code;     }      public void setcountrycode(string countrycode) {          this.country_code = countrycode;     }      public long getid() {         return id;     }      public void setid(long id) {         this.id = id;     }      @override     public string tostring() {         return "country [id=" + id.tostring() + ", country_code=" + country_code.tostring()  +"]";     } } 

city.java

package com.mahlzeit.datamodel.geographic;  import javax.persistence.entity; import javax.persistence.generatedvalue; import javax.persistence.id; import javax.persistence.joincolumn; import javax.persistence.manytoone; import javax.persistence.table;  @entity @table public class city {      @id     @generatedvalue     private long id;      @manytoone     @joincolumn(name = "country_id")     private country country;      private string name;      public string getname() {         return name;     }      public void setname(string name) {         this.name = name;     }      public country getcountry() {         return country;     }      public void setcountry(country country) {         this.country = country;     }      public long getid() {         return id;     }      public void setid(long id) {         this.id = id;     }       @override     public string tostring() {         return "city [id=" + id.tostring() + ", country="                 + country.tostring() + ", name=" + name + "]";     } } 

what have make work? @joincolumn(name = "country_id") correct in city.java?

you have change critera crcities.add(restrictions.eq("country.id", austria.getid()));.

you can't use join column. have use column in joined object.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -