休眠:无法通过反射设置器设置字段值 com.mahlzeit.datamodel.address.City.id



我正在尝试使用@ManyToOne关系向Country添加City。问题是我在尝试保存我的实体时得到了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。它肯定只是一个注释的东西..

请注意:我自动生成了所有 setter 和 getter,但为了 SO 上的代码长度而删除了它们。

国家.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()  +"]";
    }
}

城市.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

两个类之间的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

有关一对多映射的更多信息,请查看此休眠一对多注释教程

最新更新