【问题标题】:Hibernate one to many input using json with POSTMAN使用带有 POSTMAN 的 json 休眠一对多输入
【发布时间】:2016-08-12 09:54:20
【问题描述】:

问题是一对多的休眠映射在这种 json 格式中不起作用。我认为这是一个逻辑错误,没有显示语法错误。

我的控制器是:

 @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);
    }

员工实体类是:

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


} 

教育实体是:

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 输入:

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

  } 

一对多映射不适用于此json格式。如何以json格式实现此映射?请给我您宝贵的建议。

【问题讨论】:

    标签: json hibernate hibernate-onetomany


    【解决方案1】:

    使用

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

    而不是,

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-13
      • 2012-01-17
      • 1970-01-01
      相关资源
      最近更新 更多