java - issue with Criteria when making an association on 2 tables with a fereign key -


i m trying learn hibernate , criteria. have 2 tables rats , sickness. set foreign key in rats : rats.sickness_id = sickness.id.

i m trying criteria equivalent of sql:

select * rats r, sickness s s.id = r.sickness_id 

i assumed association:

session .createcriteria(rats.class) .createcriteria(sickness.class) .tolist() 

this unfortunately ends with: org.hibernate.queryexception: not resolve property: entities of: entities.rats

strange part both:

session.createcriteria(rats.class).tolist() 

and

    session.createcriteria(sickness.class).tolist()  work fine....  i'm bit puzzled. here entities classes code:  @entity @table(name = "rats") public class rats implements java.io.serializable {      private int id;     private sickness sickness;     private string name;     private int age;      public rats() {     }      public rats(int id, string name, int age) {         this.id = id;         this.name = name;         this.age = age;     }      public rats(int id, sickness sickness, string name, int age) {         this.id = id;         this.sickness = sickness;         this.name = name;         this.age = age;     }      @id     @column(name = "id", unique = true, nullable = false)     public int getid() {         return this.id;     }      public void setid(int id) {         this.id = id;     }      @manytoone(fetch = fetchtype.lazy)     @joincolumn(name = "sickness_id")     public sickness getsickness() {         return this.sickness;     }      public void setsickness(sickness sickness) {         this.sickness = sickness;     }      @column(name = "name", nullable = false, length = 50)     public string getname() {         return this.name;     }      public void setname(string name) {         this.name = name;     }      @column(name = "age", nullable = false)     public int getage() {         return this.age;     }      public void setage(int age) {         this.age = age;     }      @override     public string tostring() {         string returnstring =   "my name "   + getname() + ", " + getage()+ ". ";         returnstring += getsickness() == null ? "i healthy hell! :)" :  "i suffer " + getsickness().getnom();         return returnstring;     }     } 

and

@entity @table(name = "sickness") public class sickness implements java.io.serializable {      private int id;     private string nom;     private set<rats> ratses = new hashset<rats>(0);      public sickness() {     }      public sickness(int id) {         this.id = id;     }      public sickness(int id, string nom, set<rats> ratses) {         this.id = id;         this.nom = nom;         this.ratses = ratses;     }      @id     @column(name = "id", unique = true, nullable = false)     public int getid() {         return this.id;     }      public void setid(int id) {         this.id = id;     }      @column(name = "nom", length = 50)     public string getnom() {         return this.nom;     }      public void setnom(string nom) {         this.nom = nom;     }      @onetomany(fetch = fetchtype.lazy, mappedby = "sickness")     public set<rats> getratses() {         return this.ratses;     }      public void setratses(set<rats> ratses) {         this.ratses = ratses;     }      @override     public string tostring() {         return getnom()                 +           (   getratses() != null ? (", getratses()=" + getratses() + "]"): "" );     }    } 

what did miss? in advance.

on rats entity, sickness entity property is:

 private sickness sickness; 

accordingly, association must use same name.

session.createcriteria(rats.class)     .createcriteria("sickness")     .list(); 

one other solution, should change rats use eager fetch:

@manytoone(fetch = fetchtype.eager) @joincolumn(name = "sickness_id") public sickness getsickness() {     return this.sickness; }  public void setsickness(sickness sickness) {     this.sickness = sickness; } 

and use:

session.createcriteria(rats.class)     .list(); 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -