Beginner
Beginner

Reputation: 335

What are the constraints in using constructors in POJO while using hibernate?

I was trying a simple hibernate populate the db example. There are two POJO's Employee and Address. When I tried to use both the Employee and Address constructor's with parameters to create two instances an error could not get constructor for org.hibernate.persister.entity.singletableentitypersisterwas thrown but the property accessor methods worked fine. Why did I get the error ?

Ok since I do not have the stack trace right now I shall rephrase my question are property accessor methods preferred over constructors in hibernate?

Employee 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();
    }

} 

Address 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;
    }

}

`

Upvotes: 1

Views: 295

Answers (1)

Chetan Kinger
Chetan Kinger

Reputation: 15212

Your class should have a default public constructor that does not take any arguments. That's the only constraint with respect to constructors when using Hibernate.

As for the exception, you are probably missing a setter for one of your fields or the setters don't follow the convention expected by Hibernate. But this can only be confirmed if you provide a full stack trace.

are property accessor methods preferred over constructors in hibernate?

What do you mean by preffered? If you mean are property methods optional, then the answer is no. (Whcih could be one of the reasons for the exception in the first place)

Upvotes: 1

Related Questions