com.fasterxml.jackson.databind.JsonMappingException:名称为'defaultReference'的多个反向引用属性

时间:2017-05-15 06:47:04

标签: java spring hibernate

当我尝试使用多个实体映射一个实体时,我收到此错误。

///这是我的候选人资料实体/////////


    package com.demo.project.premiumjobportal.model;

    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.Id;
    import javax.persistence.ManyToOne;
    import javax.persistence.OneToMany;
    import javax.persistence.Table;
    import javax.validation.constraints.NotNull;
    import javax.xml.bind.annotation.XmlRootElement;

    import com.fasterxml.jackson.annotation.JsonBackReference;
    import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

    //Candidate details table : Master Table
    @Entity
    @Table(name="candidate_profile")
    @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
    @XmlRootElement
    public class CandidateProfile implements Serializable {

        private static final long serialVersionUID = 1L;

        @Id
        @GeneratedValue
        private int id;

        @NotNull
        @Column(name="first_name", length = 25)
        private String firstName;

        @NotNull
        @Column(name="last_name", length = 25)
        private String lastName;

        @NotNull
        @Column(name="username", length = 50)
        private String username;

        @Column(name="alt_email_id", length = 50)
        private String alternateEmailId;

        @NotNull
        @Column(name="mobile_no", length = 15)
        private String mobileNum;

        @Column(name="alt_mobile_no", length = 15)
        private String alternateMobileNum;


        @Column(name="ssc_board", length = 50)
        private String sscBoard;


        @Column(name="ssc_school", length = 50)
        private String sscSchool;


        @Column(name="ssc_percent", length = 4)
        private float sscPercentage;


        @Column(name="ssc_year_of_passout", length = 4)
        private int sscYearOfPassout;


        @Column(name="hsc_board", length = 50)
        private String hscBoard;


        @Column(name="hsc_school", length = 50)
        private String hscSchool;


        @Column(name="hsc_percent", length = 4)
        private float hscPercentage;


        @Column(name="hsc_year_of_passout", length = 4)
        private int hscYearOfPassout;


        @Column(name="grad_university", length = 100)
        private String gradiduteUniversity;


        @Column(name="grad_institute", length = 100)
        private String gradiduteInstitute;


        @Column(name="grad_percent", length = 4)
        private float gradidutePercentage;


        @Column(name="grad_year_of_passout", length = 4)
        private int gradiduteYearOfPassout;

        @Column(name="postgrad_university", length = 100)
        private String postGradiduteUniversity;

        @Column(name="postgrad_institute", length = 100)
        private String postGradiduteInstitute;

        @Column(name="postgrad_percent", length = 4)
        private float postGradidutePercentage;

        @Column(name="postgrad_year_of_passout", length = 4)
        private int postGradiduteYearOfPassout;

        private String notes;

        private String Password;

        @OneToMany(mappedBy = "candidateProfile", fetch = FetchType.LAZY,cascade = CascadeType.REMOVE)
        @JsonBackReference
        private List experienceDetails;

        @OneToMany(mappedBy = "candidateProfile", fetch = FetchType.LAZY,cascade = CascadeType.REMOVE)
        @JsonBackReference
        private List candidateJobs;

        @ManyToOne
        private Role roleName;



        /* Constructor */
        public CandidateProfile() {
            super();
            }


        // Getters and Setters 

///这是我的经验详情实体////////////


    package com.cfa.project.premiumjobportal.model;

    import java.io.Serializable;

    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import javax.persistence.ManyToOne;
    import javax.persistence.Table;
    import javax.xml.bind.annotation.XmlRootElement;

    import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

    //Experience class, which connected with CandidateProfile.class
    @Entity
    @Table(name="experience_details")
    @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
    @XmlRootElement
    public class ExperienceDetails implements Serializable {

        private static final long serialVersionUID = 1L;

        @Id
        @GeneratedValue
        private int id;

        @Column(name = "tot_experenice", length = 2)
        private String totExperience;

        @Column(name = "companies_month_experience")
        private String companiesMonthExperience;

        @Column(name = "skills")
        private String _skills;

        @Column(name = "extra_curricular")
        private String extraCurricular;

        private String notes;

        /* Mapping : Joining with Candidate table */
        @ManyToOne
        private CandidateProfile candidateProfile;

        /*Constructor*/
        public ExperienceDetails() {
            super();
        }


        /*Constructor using fields*/
        /**
         * @param id
         * @param totExperience
         * @param companiesMonthExperience
         * @param _skills
         * @param extraCurricular
         * @param notes
         * @param candidateProfile
         */
        public ExperienceDetails(int id, String totExperience, String companiesMonthExperience, String _skills,
                String extraCurricular, String notes, CandidateProfile candidateProfile) {
            super();
            this.id = id;
            this.totExperience = totExperience;
            this.companiesMonthExperience = companiesMonthExperience;
            this._skills = _skills;
            this.extraCurricular = extraCurricular;
            this.notes = notes;
            this.candidateProfile = candidateProfile;
        }

        /* Generating Getters and setters */

///这是我的角色实体//////


    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.Id;
    import javax.persistence.OneToMany;
    import javax.persistence.Table;
    import javax.xml.bind.annotation.XmlRootElement;

    import com.fasterxml.jackson.annotation.JsonBackReference;
    import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

    @Entity
    @Table(name="role")
    @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
    @XmlRootElement
    public class Role implements Serializable {

        private static final long serialVersionUID = 1L;

        @Id
        @GeneratedValue
        private int id;

        @Column(length = 10)
        private String name;

        @OneToMany(mappedBy = "roleName", fetch = FetchType.LAZY,cascade = CascadeType.REMOVE)
        @JsonBackReference
        private List candidateProfiles;

        @OneToMany(mappedBy = "companyRoleName", fetch = FetchType.LAZY,cascade = CascadeType.REMOVE)
        @JsonBackReference
        private List recruiterDetails;

我得到的这个错误---------------- 无法为类型[[simple type,class com.demo.project.premiumjobportal.model.CandidateProfile]]评估Jackson反序列化:com.fasterxml.jackson.databind.JsonMappingException:名称为'defaultReference'的多个反向引用属性

无法评估类型[[simple type,class com.demo.project.premiumjobportal.model.CandidateProfile]]的Jackson反序列化:com.fasterxml.jackson.databind.JsonMappingException:Multiple back-名为'defaultReference'的引用属性

请帮我解决这个问题。我到处搜索,我尝试了所有解决方案但是我也遇到了这个错误。

1 个答案:

答案 0 :(得分:4)

如果在项目中的getter / setter方法上使用@JsonBackReference两次以上,则应使用特定的引用名称区分它们。

Role实体类中,您已使用@JsonBackReference两次

@OneToMany(mappedBy = "roleName", fetch = FetchType.LAZY,cascade = CascadeType.REMOVE)
    @JsonBackReference
    private List candidateProfiles;

    @OneToMany(mappedBy = "companyRoleName", fetch = FetchType.LAZY,cascade = CascadeType.REMOVE)
    @JsonBackReference
    private List recruiterDetails;

您应该将其更改为 -

@OneToMany(mappedBy = "roleName", fetch = FetchType.LAZY,cascade = CascadeType.REMOVE)
    @JsonBackReference(value="candidate-profiles")
    private List candidateProfiles;

    @OneToMany(mappedBy = "companyRoleName", fetch = FetchType.LAZY,cascade = CascadeType.REMOVE)
    @JsonBackReference(value="recruiter-details")
    private List recruiterDetails;