Search code examples
jsonhibernatehibernate-onetomany

Hibernate one to many input using json with POSTMAN


The problem is one to many hibernate mapping is not working in this json format. I think it's a logical error, syntax error is not shown.

My Controller is:

 @RequestMapping(value = "/save", method = RequestMethod.POST, produces =MediaType.APPLICATION_JSON_VALUE,headers="Accept=application/json,application/xml")
    public @ResponseBody JsonRecord setCurrentDataList(@RequestBody Employee emp) {
        try {

            int id=employeeServices.save(emp);

        } catch (Exception e) {

            return new JsonRecord(false,e.getMessage());

        }
        return new JsonRecord(true,"Successful",emp);
    }

Employee Entity Class is:

import java.io.Serializable;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.annotations.IndexColumn;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import com.fasterxml.jackson.annotation.JsonProperty;


@Entity
@Table(name="Employee")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
@JsonAutoDetect(getterVisibility=JsonAutoDetect.Visibility.NONE)
public class Employee implements Serializable{

    private static final long serialVersionUID = -723583058586873479L;

    @Id
    @GeneratedValue
    @Column(name ="empId")
    @JsonProperty("empId")
    private Integer empId;

    @JsonProperty("empName")
    private String empName;

    @JsonProperty("empAddress")
    private String empAddress;

    @JsonProperty("salary")
    private double salary;

    @JsonProperty("empAge")
    private Integer empAge;

    @OneToMany(mappedBy="employee",cascade=CascadeType.ALL,fetch=FetchType.EAGER)
    @Fetch(FetchMode.SELECT)
    private List<Education> education;


    public Integer getEmpId() {
        return empId;
    }

    public void setEmpId(Integer empId) {
        this.empId = empId;
    }

    public String getEmpName() {
        return empName;
    }

    public void setEmpName(String empName) {
        this.empName = empName;
    }

    public String getEmpAddress() {
        return empAddress;
    }

    public void setEmpAddress(String empAddress) {
        this.empAddress = empAddress;
    }

    public double getSalary() {
        return salary;
    }

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

    public Integer getEmpAge() {
        return empAge;
    }

    public void setEmpAge(Integer empAge) {
        this.empAge = empAge;
    }

    @OneToMany(fetch = FetchType.EAGER, mappedBy = "employee")
    @JsonManagedReference
    public List<Education> getEducation() {
        return education;
    }

    public void setEducation(List<Education> education) {
        this.education = education;
    }


} 

Education Entity is:

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.codehaus.jackson.annotate.JsonIgnoreProperties;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonProperty;


@Entity
@Table(name="Education")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
@JsonAutoDetect(getterVisibility=JsonAutoDetect.Visibility.NONE)
public class Education{

    @Id
    @GeneratedValue
    @Column(name ="eduID")
    @JsonProperty("eduID")
    private int eduID;

    @JsonProperty("qualification")
    private String qualification;

    @JsonProperty("stream")
    private String stream;

    @ManyToOne
    @JoinColumn(name="empid")
    private Employee employee;


    public int getEduID() {
        return eduID;
    }

    public void setEduID(int eduID) {
        this.eduID = eduID;
    }

    public String getQualification() {
        return qualification;
    }

    public void setQualification(String qualification) {
        this.qualification = qualification;
    }

    public String getStream() {
        return stream;
    }

    public void setStream(String stream) {
        this.stream = stream;
    }
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "empId", nullable = false)
    @JsonBackReference
    public Employee getEmployee() {
        return employee;
    }

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


}

JSON input:

{
    "empName": "myname",
    "empAddress": "my address",
    "salary": 1000,
    "empAge": 24,
    "education":[{
        "qualification":"mca",
        "stream":"mca"
    }]

  } 

One to many mapping is not working with this json format.How to implement this mapping in json format? Please give me your valuable suggestions.


Solution

  • use

    @OneToMany(cascade={CascadeType.ALL})
    @Fetch(FetchMode.JOIN)
    @JoinColumn(name="empId", referencedColumnName="empId")
    private Set<Education> education;
    

    instead of,

    @OneToMany(mappedBy="employee",cascade=CascadeType.ALL,fetch=FetchType.EAGER)
        @Fetch(FetchMode.SELECT)
        private List<Education> education;