2015-04-29 37 views
1

我有这个简单的关系 - 如可以在图片中可以看出:简单@ManyToOne关系:无法解析属性(org.hibernate.QueryException)

enter image description here

当我尝试读取读出某些城市具有一定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()); 
} 

,我发现了以下错误:

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

这是POJO的:

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 + "]"; 
    } 
} 

我有什么做的,使这项工作?是@JoinColumn(name = "Country_id")更正确在City.java

+2

你有一个错字:'Counry_id'。你错过了一个't'。 – Jens

+0

@Jens Thx。修正了它,但同样的错误发生。 – displayname

+0

我认为你的标准必须是'crCities.add(Restrictions.eq(“country.id”,austria.getId()));' – Jens

回答

0

你必须改变你的标准为crCities.add(Restrictions.eq("country.id", austria.getId()));

您不能使用连接列。您必须使用连接对象中的列。

2

您的标准有以下限制:

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

这意味着一个名为Country_id属性,休眠搜索。你的班级City没有这样的属性。它只有一个名为country的属性。

所以既可以使用与

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

的对象模型,它是下休眠的首选方式。

或者,您也可以使用更SQL十岁上下的方式与

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

当你明确地说,你要使用的财产countryid

结果和创建的SQL语句应该是相同的。

相关问题