使用hibernate时在POJO中使用构造函数有哪些限制?

时间:2015-02-27 10:43:25

标签: java hibernate

我正在尝试一个简单的hibernate填充db示例。有两个POJO的员工和地址。当我尝试使用带有参数的Employee和Address构造函数创建两个实例时,抛出了错误could not get constructor for org.hibernate.persister.entity.singletableentitypersister,但属性访问器方法工作正常。为什么我收到错误?

好的,因为 我现在没有堆栈跟踪 我将重新解释我的问题是属性访问器方法优于hibernate中的构造函数吗?

员工POJO:

package many2one;

public class Employee {

    public int id;
    public String firstName;
    public String lastName;
    public int salary;
    public Address address;

    public Employee(){}
    public Employee(String firstName,String lastName,int salary,Address address){
        this.firstName = firstName;
        this.lastName = lastName;
        this.salary = salary;
        this.address = address;
    }

    public int getId(){
        return id;
    }

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

    public String getFirstName(){
        return firstName;
    }

    public void setFirstName(String fname){
        this.firstName = fname;
    }

    public String getLastName(){
        return lastName;
    }

    public void setLastName(String lname){
        this.lastName = lname;
    }

    public int getSalary(){
        return salary;
    }

    public void setSalary(int salary){
        this.salary = salary;
    }

    public Address getAddress(){
        return address;
    }

    public void setAddress(Address address){
        this.address = address;
    }
    @Override
    public String toString(){
        return id+","+firstName+","+lastName+","+salary+","+address.getStreetName()+","+address.getCityName()+","+address.getStateName()+","+address.getZipcode();
    }

} 

地址POJO:

package many2one;

public class Address {
    public int id;
    public String streetName;
    public String cityName;
    public String stateName;
    public String zipcode;

    public Employee employee;

    public Address(){

    }

    public Address(String sname,String cname,String statename,String zipcode){
        this.streetName = sname;
        this.cityName = cname;
        this.stateName = statename;
        this.zipcode = zipcode;
    }

    public int getId(){
        return id;
    }

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

    public String getStreetName(){
        return streetName;
    }

    public void setStreetName(String streetname){
        this.streetName = streetname;
    }

    public String getCityName(){
        return cityName;
    }

    public void setCityName(String cname){
        this.cityName = cname;
    }    

    public String getStateName(){
        return stateName;
    }

    public void setStateName(String statename){
        this.stateName = statename;
    }

    public String getZipcode(){
        return zipcode;
    }

    public void setZipcode(String zipcode){
        this.zipcode = zipcode;
    }

    public Employee getEmployee(){
        return employee;
    }

    public void setEmployee(Employee employee){
        this.employee = employee;
    }

}

`

1 个答案:

答案 0 :(得分:1)

您的类应该有一个不带任何参数的默认公共构造函数。这是使用Hibernate时构造函数的唯一约束。

对于异常,您可能缺少某个字段的setter,或者setter不遵循Hibernate期望的约定。但只有在提供完整的堆栈跟踪时才能确认这一点。

  

是属于优先于构造函数的属性访问器方法   冬眠?

你的意思是什么?如果你的意思是属性方法是可选的,那么答案是否定的。 (Whcih可能是首先例外的原因之一)