2016-08-12 8 views
1

Проблема в том, что в этом json-формате не работает одно из нескольких сопоставлений спящего режима. Я думаю, что это логическая ошибка, синтаксическая ошибка не показана.Hibernate от одного до многих входных данных с использованием json с POSTMAN

Мой контроллер:

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

Сотрудник Entity Класс:

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


} 

Образование Entity является:

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? Пожалуйста, дайте мне ваши ценные предложения.

ответ

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;