我尝试使用City
关系向Country
添加@ManyToOne
。问题是我在尝试保存实体时收到org.hibernate.PropertyAccessException
。
private void addAddressData() {
Session session = sessionFactory.openSession();
session.beginTransaction();
List<Country> countryList = new ArrayList<>();
Country austria = new Country("at");
countryList.add(new Country("de"));
countryList.add(austria);
for(Country country : countryList) {
try {
session.save(country);
} catch (ConstraintViolationException e) {
e.printStackTrace();
}
}
List<City> cityList = new ArrayList<>();
cityList.add(new City(austria, "Graz"));
cityList.add(new City(austria, "Wien"));
for(City city : cityList) {
try {
session.save(city);
} catch (ConstraintViolationException e) {
e.printStackTrace();
}
}
session.getTransaction().commit();
}
似乎我需要指定不同的关系,或者至少告诉Hibernate它可以找到Country
id。它肯定只是一个注释事物..
请注意:我自动生成了所有的设置器和getter,但是为了代码长度,在SO上删除它们。
Country.java
@Entity
public class Country implements Serializable {
/**
*
*/
private static final long serialVersionUID = -2060021861139912774L;
@Id
@GeneratedValue
@Column(name="id")
private Long id; // = null; (was not the problem)
@Column(unique=true)
private String countryCode;
public Country(String country_code) {
this.countryCode = country_code;
}
public String getCountryCode() {
return countryCode;
}
public void setCountryCode(String countryCode) {
this.countryCode = 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=" + countryCode.toString() +"]";
}
}
City.java
@Entity
public class City implements Serializable {
/**
*
*/
private static final long serialVersionUID = -4374113410767348574L;
@Id
@GeneratedValue
@Column(name="id")
private Long id;
@Id
@ManyToOne
private Country country;
private String cityName;
public City(Country country, String cityName) {
this.setCountry(country);
this.cityName = cityName;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getCityName() {
return cityName;
}
public void setCityName(String countryName) {
this.cityName = countryName;
}
public Country getCountry() {
return country;
}
public void setCountry(Country country) {
this.country = country;
}
@Override
public String toString() {
return "City [id=" + id.toString() + ", country_id="
+ getCountry().toString() + ", cityName=" + cityName + "]";
}
}
堆栈跟踪
Exception in thread "main" org.hibernate.PropertyAccessException: could not set a field value by reflection setter of com.mahlzeit.datamodel.address.City.id
at org.hibernate.property.DirectPropertyAccessor$DirectSetter.set(DirectPropertyAccessor.java:150)
at org.hibernate.mapping.Component$ValueGenerationPlan.execute(Component.java:436)
at org.hibernate.id.CompositeNestedGeneratedValueGenerator.generate(CompositeNestedGeneratedValueGenerator.java:121)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:120)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:204)
at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:55)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:189)
at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:49)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:642)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:635)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:631)
at com.mahlzeit.datamodel.HibernateTest.addAddressData(HibernateTest.java:57)
at com.mahlzeit.datamodel.HibernateTest.main(HibernateTest.java:31)
Caused by: java.lang.IllegalArgumentException: Can not set java.lang.Long field com.mahlzeit.datamodel.address.City.id to org.hibernate.id.IdentifierGeneratorHelper$2
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source)
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source)
at sun.reflect.UnsafeObjectFieldAccessorImpl.set(Unknown Source)
at java.lang.reflect.Field.set(Unknown Source)
at org.hibernate.property.DirectPropertyAccessor$DirectSetter.set(DirectPropertyAccessor.java:138)
... 13 more
答案 0 :(得分:5)
两个类之间oneToMany
映射的定义不正确。
@Id //wrong annotation here
@ManyToOne
private Country country;
应该是:
@ManyToOne
@JoinColumn(name="id") //the country id, better rename it country_id
private Country country;
@Id
在这里放错了位置,Hibernate感到困惑,因为有两列注释@Id
,在Country
类中,您应该声明一组城市来完成映射,例如这样:
@OneToMany(mappedBy="country")
private Set<City> cities;
//the right getters and setters for cities
有关“一对多”映射的详细信息,请查看此Hibernate One To Many Annotation Tutorial。