无法将int字段com.java.hibernate.practise.User.id设置为java.util.HashSet

时间:2014-08-11 19:36:23

标签: java hibernate jpa

I have hibernate @OneToMany mapping I am getting the mentioned error. Does not understand the reason. As getters and setters are public

Below are the entities

@Entity
@Table(name="USER_DETAILS")
public class User implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Id
    @Column(name="USER_ID")
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int id;

    @Column(name="USER_FIRSTNAME",nullable=false, length=50)
    private String userFirstName;

    @Column(name="USER_LASTNAME",nullable=false, length=50)
    private String userLastName;

    @Column(name="USER_MIDDLENAME",length = 30)
    private String userMiddleName;

    @Column(name="USER_AGE")
    private int userAge;

    @Column(name="USER_SEX")
    private String userSex;


    @OneToMany(cascade=CascadeType.ALL, mappedBy="userAddress", targetEntity=Address.class)
    private Set<Address> address = new HashSet<Address>();

    public String getUserFirstName() {
        return userFirstName;
    }

    public void setUserFirstName(String userFirstName) {
        this.userFirstName = userFirstName;
    }

    public String getUserLastName() {
        return userLastName;
    }

    public void setUserLastName(String userLastName) {
        this.userLastName = userLastName;
    }

    public String getUserMiddleName() {
        return userMiddleName;
    }

    public void setUserMiddleName(String userMiddleName) {
        this.userMiddleName = userMiddleName;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getUserAge() {
        return userAge;
    }

    public void setUserAge(int userAge) {
        this.userAge = userAge;
    }

    public String getUserSex() {
        return userSex;
    }

    public void setUserSex(String userSex) {
        this.userSex = userSex;
    }

    public Set<Address> getAddress() {
        return address;
    }

    public void setAddress(Set<Address> address) {
        this.address = address;
    }


}

@Entity
@Table(name = "ADDRESS")
public class Address implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Id
    @Column(name = "ADDRESS_ID")
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int id;

    @Column(name = "ZIP_CODE")
    private String zipCode;

    @Column(name="ADDRESS_USER_ID", insertable=false, updatable=false)
    private Long addressUserID;

    @Column(name = "ADDRESS_SEC")
    private String addressSec;

    @Column(name = "STREET")
    private String street;

    @Column(name = "CITY")
    private String city;

    @Column(name = "COUNTRY")
    private String country;

    @ManyToOne(cascade=CascadeType.ALL, targetEntity=User.class)
    @JoinColumn(name="ADDRESS_USER_ID")
    private Set<User> userAddress = new HashSet<User>();

    public Long getAddressUserID() {
        return addressUserID;
    }

    public void setAddressUserID(Long addressUserID) {
        this.addressUserID = addressUserID;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getZipCode() {
        return zipCode;
    }

    public void setZipCode(String zipCode) {
        this.zipCode = zipCode;
    }

    public Set<User> getUserAddress() {
        return userAddress;
    }

    public void setUserAddress(Set<User> userAddress) {
        this.userAddress = userAddress;
    }


    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        this.street = street;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getAddressSec() {
        return addressSec;
    }

    public void setAddressSec(String addressSec) {
        this.addressSec = addressSec;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

}

    Part of Stack Trace are:

线程“main”org.hibernate.PropertyAccessException中的异常:无法通过com.java.hibernate.practise.User.id 的反射getter获取... org.hibernate的字段值.property.DirectPropertyAccessor $ DirectGetter.get(DirectPropertyAccessor.java:62) 引起:java.lang.IllegalArgumentException:无法将int字段com.java.hibernate.practise.User.id设置为java.util.HashSet ...

我使用hibernate.hbm2ddl.auto = cerate-drop
生成模式 请指导一下。

1 个答案:

答案 0 :(得分:0)

一般来说,当我们使用1..n双向实体映射时,一般来说,拥有的一方,多边,应该只有一个实例引用一边的对象(不是一个集合 - 这将是多对多的),并且要使用的连接列是on on class类的主键。我们不需要像你一样在许多方面明确使用FK列。

所以如果这是你的关系User [1]..[N] Address,那么你应该有更像

的东西
public class User {
    ...
    @OneToMany(mappedBy = "user", cascade=CascadeType.ALL)
    private Set<Address> addresses;
}

public class Address {
    // private Long addressUserID; // Don't need this property. We get it below
    ...
    @ManyToOne
    @JoinColumn(name = "USER_ID")
    private User user;
}