Stefan Falk
Stefan Falk

Reputation: 25407

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

I have this simple relationship - as can be seen in the picture:

enter image description here

As I try to read read out a certain city with a certain Country_id:

Country austria = (Country) session.load(Country.class, 1L); 
// Works as 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:");
for (City next : cities) {
    System.out.println(" - " + next.getName());
}  

I'm getting the following error:

Exception in thread "main" org.hibernate.QueryException: could not resolve property: Country_id of: com.mahlzeit.datamodel.geographic.City

This are the 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 do I have to do to make this work? Is @JoinColumn(name = "Country_id") even correct in City.java?

Upvotes: 1

Views: 3355

Answers (2)

Uwe Plonus
Uwe Plonus

Reputation: 9954

Your criteria has the following restriction:

crCities.add(Restrictions.eq("Country_id", austria.getId()));

This means that hibernate searches for a property named Country_id. Your class City does not have such a property. It only has a property named country.

so either use the object model with

crCities.add(Restrictions.eq("country", austria));

which is the prefered way under hibernate.

Alternatively you can use a more SQLish way with

crCities.add(Restrictions.eq("country.id", austria.getId()));

Where you say explicitly that you want to use the property id of country.

The result and the created SQL statements should be the same.

Upvotes: 2

Jens
Jens

Reputation: 69450

You have to change your critera to crCities.add(Restrictions.eq("country.id", austria.getId()));.

You can't use the join column. You have to use the column in the joined object.

Upvotes: 1

Related Questions