Search code examples
javahibernatecriteria

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


I m trying to learn about hibernate and Criteria. i have 2 tables RATS and SICKNESS. I set a foreign key in RATS : RATS.Sickness_Id = SICKNESS.ID.

I m trying to get with Criteria an equivalent of SQL:

select * from RATS r, SICKNESS s where s.id = r.sickness_id

I assumed it was this association:

session
.createCriteria(Rats.class)
.createCriteria(Sickness.class)
.toList()

This unfortunately ends up with: org.hibernate.QueryException: could not resolve property: entities of: entities.Rats

Strange part is that both:

session.createCriteria(Rats.class).toList()

and

    session.createCriteria(Sickness.class).toList()

work fine....

I'm a bit puzzled.
Here are my 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 is "   + getName() + ", I am " + getAge()+ ". ";
        returnString += getSickness() == null ? "I am healthy like hell! :)" :  "I suffer from " + 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 I miss? Thanks in advance.


Solution

  • On Rats entity, the Sickness entity property is:

     private Sickness sickness;
    

    Accordingly, your association must use the same name.

    session.createCriteria(Rats.class)
        .createCriteria("sickness")
        .list();
    

    One other solution, should be to change Rats to 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 then use:

    session.createCriteria(Rats.class)
        .list();