如何禁用无限循环双向映射

问题描述 投票:0回答:1

我使用了hibernate工具插件(sts)从oracle数据库生成我的实体。.然后,我创建了用于登录的post方法,该方法返回一个用户对象,但是我的用户(Utilisateur)与具有与配置文件类型双向关系的配置文件相关所以它造成了一个循环。我在Profil.typeProfil上尝试了JsonIgnore,但没有解决它,然后我尝试了@jsonManagedReference和JsonBackReference它的显示:“未找到类org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor的序列化程序,也未找到创建BeanSerialize的属性”我也尝试反映JsonManagedReference和JsonBackReference批注,但是当我在邮递员上尝试它时,它变成黑色,并且我认为这是由于无限循环所致

这是json的一部分

"login": "admin",
    "profil": {
        "idProfil": 1,
        "profilType": {
            "uniteType": {
                "typeUnite": "CSC",
                "prmOtTypesForCTypeUniteSec": [],
                "prmOtTypesForCTypeUnite": [],
                "ctypeUnite": "CSC"
            },
            "typeProfil": "Administrateur",
            "profils": [
                {
                    "idProfil": 1,
                    "profilType": {
                        "uniteType": {
                            "typeUnite": "CSC",
                            "prmOtTypesForCTypeUniteSec": [],
                            "prmOtTypesForCTypeUnite": [],
                            "ctypeUnite": "CSC"
                        },
                        "typeProfil": "Administrateur",
                        "profils": [
                            {
                                "idProfil": 1,
                                "profilType": {
                                    "uniteType": {
                                        "typeUnite": "CSC",
                                        "prmOtTypesForCTypeUniteSec": [],
                                        "prmOtTypesForCTypeUnite": [],
                                        "ctypeUnite": "CSC"
                                    },......



@Entity
@Table(name = "PROFIL_TYPE", schema = "TOM")
public class ProfilType implements java.io.Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private String CTypeProfil;
    @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
    private UniteType uniteType;

    private String typeProfil;
    private boolean FCentral;
    @JsonIgnore
    private Set<FonctionProftype> fonctionProftypes = new HashSet<FonctionProftype>(0);
    @JsonBackReference
    private Set<Profil> profils = new HashSet<Profil>(0);

    @Id
    @Column(name = "C_TYPE_PROFIL", unique = true, nullable = false, length = 20)
    public String getCTypeProfil() {
        return this.CTypeProfil;
    }

    public void setCTypeProfil(String CTypeProfil) {
        this.CTypeProfil = CTypeProfil;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "C_TYPE_UNITE")
    public UniteType getUniteType() {
        return this.uniteType;
    }

    public void setUniteType(UniteType uniteType) {
        this.uniteType = uniteType;
    }

    @Column(name = "TYPE_PROFIL", nullable = false, length = 100)
    public String getTypeProfil() {
        return this.typeProfil;
    }

    public void setTypeProfil(String typeProfil) {
        this.typeProfil = typeProfil;
    }

    @Column(name = "F_CENTRAL", nullable = false, precision = 1, scale = 0)
    public boolean isFCentral() {
        return this.FCentral;
    }

    public void setFCentral(boolean FCentral) {
        this.FCentral = FCentral;
    }

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "profilType")
    public Set<FonctionProftype> getFonctionProftypes() {
        return this.fonctionProftypes;
    }

    public void setFonctionProftypes(Set<FonctionProftype> fonctionProftypes) {
        this.fonctionProftypes = fonctionProftypes;
    }

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "profilType")

    public Set<Profil> getProfils() {
        return this.profils;
    }

    public void setProfils(Set<Profil> profils) {
        this.profils = profils;
    }



@Entity
@Table(name = "PROFIL", schema = "TOM")
public class Profil implements java.io.Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private BigDecimal idProfil;

    private ProfilType profilType;
    private String profil;
    private String descProfil;
    private boolean FEtat;
    private Date DEtat;
    private String loginEtat;
    private Set<Fonction> fonctions = new HashSet<Fonction>(0);
    private Set<Utilisateur> utilisateurs = new HashSet<Utilisateur>(0);

    @Id

    @Column(name = "ID_PROFIL", unique = true, nullable = false, precision = 22, scale = 0)
    public BigDecimal getIdProfil() {
        return this.idProfil;
    }

    public void setIdProfil(BigDecimal idProfil) {
        this.idProfil = idProfil;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "C_TYPE_PROFIL", nullable = false)
    @JsonManagedReference
    public ProfilType getProfilType() {
        return this.profilType;
    }

    public void setProfilType(ProfilType profilType) {
        this.profilType = profilType;
    }

    @Column(name = "PROFIL", nullable = false, length = 100)
    public String getProfil() {
        return this.profil;
    }

    public void setProfil(String profil) {
        this.profil = profil;
    }

    @Column(name = "DESC_PROFIL", length = 500)
    public String getDescProfil() {
        return this.descProfil;
    }

    public void setDescProfil(String descProfil) {
        this.descProfil = descProfil;
    }

    @Column(name = "F_ETAT", nullable = false, precision = 1, scale = 0)
    public boolean isFEtat() {
        return this.FEtat;
    }

    public void setFEtat(boolean FEtat) {
        this.FEtat = FEtat;
    }

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "D_ETAT", length = 11)
    public Date getDEtat() {
        return this.DEtat;
    }

    public void setDEtat(Date DEtat) {
        this.DEtat = DEtat;
    }

    @Column(name = "LOGIN_ETAT", length = 20)
    public String getLoginEtat() {
        return this.loginEtat;
    }

    public void setLoginEtat(String loginEtat) {
        this.loginEtat = loginEtat;
    }

    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "DROIT_ACCES", schema = "TOM", joinColumns = {
            @JoinColumn(name = "ID_PROFIL", nullable = false, updatable = false) }, inverseJoinColumns = {
                    @JoinColumn(name = "C_FONCTION", nullable = false, updatable = false) })
    public Set<Fonction> getFonctions() {
        return this.fonctions;
    }

    public void setFonctions(Set<Fonction> fonctions) {
        this.fonctions = fonctions;
    }

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "profil")
    public Set<Utilisateur> getUtilisateurs() {
        return this.utilisateurs;
    }

    public void setUtilisateurs(Set<Utilisateur> utilisateurs) {
        this.utilisateurs = utilisateurs;
    }

这是Utilisateur实体

@Entity
@Table(name = "UTILISATEUR", schema = "TOM")
public class Utilisateur implements java.io.Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private String login;
    private Profil profil;
    private Region region;
    private Unite unite;
    private String motPasse;
    private boolean FAd;
    private String emplacementAd;
    private String matricule;
    private String nom;
    private String prenom;
    private String email;
    private String tel;
    private boolean FEtat;
    private Date DEtat;
    private Date DSynchroAd;
    private Date DConOff;
    private Set<Equipe> equipes = new HashSet<Equipe>(0);


    @Id

    @Column(name = "LOGIN", unique = true, nullable = false, length = 20)
    public String getLogin() {
        return this.login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "ID_PROFIL", nullable = false)
    public Profil getProfil() {
        return this.profil;
    }

    public void setProfil(Profil profil) {
        this.profil = profil;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "C_REGION")
    public Region getRegion() {
        return this.region;
    }

    public void setRegion(Region region) {
        this.region = region;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "C_UNITE")
    public Unite getUnite() {
        return this.unite;
    }

    public void setUnite(Unite unite) {
        this.unite = unite;
    }

    @Column(name = "MOT_PASSE", nullable = false, length = 200)
    public String getMotPasse() {
        return this.motPasse;
    }

    public void setMotPasse(String motPasse) {
        this.motPasse = motPasse;
    }

    @Column(name = "F_AD", nullable = false, precision = 1, scale = 0)
    public boolean isFAd() {
        return this.FAd;
    }

    public void setFAd(boolean FAd) {
        this.FAd = FAd;
    }

    @Column(name = "EMPLACEMENT_AD", length = 100)
    public String getEmplacementAd() {
        return this.emplacementAd;
    }

    public void setEmplacementAd(String emplacementAd) {
        this.emplacementAd = emplacementAd;
    }

    @Column(name = "MATRICULE", length = 10)
    public String getMatricule() {
        return this.matricule;
    }

    public void setMatricule(String matricule) {
        this.matricule = matricule;
    }

    @Column(name = "NOM", nullable = false, length = 100)
    public String getNom() {
        return this.nom;
    }

    public void setNom(String nom) {
        this.nom = nom;
    }

    @Column(name = "PRENOM", nullable = false, length = 100)
    public String getPrenom() {
        return this.prenom;
    }

    public void setPrenom(String prenom) {
        this.prenom = prenom;
    }

    @Column(name = "EMAIL", length = 50)
    public String getEmail() {
        return this.email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Column(name = "TEL", length = 20)
    public String getTel() {
        return this.tel;
    }

    public void setTel(String tel) {
        this.tel = tel;
    }

    @Column(name = "F_ETAT", nullable = false, precision = 1, scale = 0)
    public boolean isFEtat() {
        return this.FEtat;
    }

    public void setFEtat(boolean FEtat) {
        this.FEtat = FEtat;
    }

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "D_ETAT", length = 11)
    public Date getDEtat() {
        return this.DEtat;
    }

    public void setDEtat(Date DEtat) {
        this.DEtat = DEtat;
    }

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "D_SYNCHRO_AD", length = 11)
    public Date getDSynchroAd() {
        return this.DSynchroAd;
    }

    public void setDSynchroAd(Date DSynchroAd) {
        this.DSynchroAd = DSynchroAd;
    }

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "D_CON_OFF", length = 11)
    public Date getDConOff() {
        return this.DConOff;
    }

    public void setDConOff(Date DConOff) {
        this.DConOff = DConOff;
    }

    @ManyToMany(fetch = FetchType.LAZY, mappedBy = "utilisateurs")
    public Set<Equipe> getEquipes() {
        return this.equipes;
    }

    public void setEquipes(Set<Equipe> equipes) {
        this.equipes = equipes;
    }

控制器方法

@PostMapping("/login")
Utilisateur authentification(@RequestBody AuthDto auth) {
    String hashedPassword = StringUtilities.hashPassword(auth.getMotPasse());
  return  us.getUserByLogin(auth.getLogin(), hashedPassword) ;
}
json spring-boot hibernate-mapping
1个回答
0
投票
在响应@JsonIgnore上不需要使用Profile.profilTypeProfilType.profils上的JSON。>
© www.soinside.com 2019 - 2024. All rights reserved.