我有这个简单的关系 - 如可以在图片中可以看出:简单@ManyToOne关系:无法解析属性(org.hibernate.QueryException)
当我尝试读取读出某些城市具有一定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?
你有一个错字:'Counry_id'。你错过了一个't'。 – Jens
@Jens Thx。修正了它,但同样的错误发生。 – displayname
我认为你的标准必须是'crCities.add(Restrictions.eq(“country.id”,austria.getId()));' – Jens