gpt4 book ai didi

java - 简单的@ManyToOne 关系 : could not resolve property (org. hibernate.QueryException)

转载 作者:行者123 更新时间:2023-11-29 05:18:05 24 4
gpt4 key购买 nike

我有这个简单的关系 - 如图所示:

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:

国家.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 中是否正确?

最佳答案

您的条件有以下限制:

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

这意味着 hibernate 搜索名为 Country_id 的属性。您的类 City 没有这样的属性。它只有一个名为 country 的属性。

所以要么使用对象模型

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

这是 hibernate 下的首选方式。

或者,您可以使用更SQL的方式

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

您明确表示要使用 country 的属性 id

结果和创建的SQL语句应该是一样的。

关于java - 简单的@ManyToOne 关系 : could not resolve property (org. hibernate.QueryException),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29936483/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com