jackson Spring,Jpa:列表包含值时出现一对多错误

cbeh67ev  于 2022-11-08  发布在  Spring
关注(0)|答案(1)|浏览(130)

我想返回一个JSON格式的Profile对象,其中包含与社交网络关联的登录详细信息列表。
当“reseaux_sociaux”表为空时,一切都正常工作。对于我的状态表,我在我的配置文件对象中以JSON格式获取我的状态。但是,当“reseaux_sociaux”包含值时,我会收到下面的错误,并且JSON格式的配置文件对象不会返回...
(记录档)
https://cloudvyzor.com/logpad/?query&database=sandbox-7fb06b2c06f198a7c0e4ff7c74d659e0
配置文件类

@Entity
@Table(name = "Profil")
public class Profil {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long Id;

    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "IdComptes", nullable = false)
    private Comptes IdComptes;
    private String Avatar;
    private String Banniere;
    private String Pseudo;
    private String MailPro;
    private String Bio;

    @ManyToMany(cascade = CascadeType.ALL,fetch = FetchType.EAGER)
    @JoinTable(name = "Statut_Profil", joinColumns = @JoinColumn(referencedColumnName = "Id"),inverseJoinColumns = @JoinColumn(referencedColumnName ="Id"))
    private List<Statut> Statut;

    @OneToMany( mappedBy = "IdProfil")
    @JsonManagedReference("id_profil")
    private List<ReseauxSociaux> Reseaux;

    public Profil(){}

    public Profil(Long id, Comptes idComptes, String avatar, String banniere, String pseudo, String mailPro, String bio) {
        Id = id;
        IdComptes = idComptes;
        Avatar = avatar;
        Banniere = banniere;
        Pseudo = pseudo;
        MailPro = mailPro;
        Bio = bio;
    }
}

社会研究类

@Entity
@IdClass(ReseauxId.class)
public class ReseauxSociaux {
    @Id
    private int Id;
    @Id
    private Long IdProfil;

    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "IdProfil", insertable = false, updatable = false)
    @JsonBackReference("id_profil")
    private Profil Profil;

    @ManyToOne
    @JoinColumn(name = "Id", insertable = false, updatable = false)
    @JsonBackReference("id")
    private Reseau Reseau;

    private String Identifiant;

    private ReseauxSociaux()
    {}

    public ReseauxSociaux(int id, Long idProfil, String identifiant) {
        Id = id;
        IdProfil = idProfil;

        Identifiant = identifiant;
    }
}

网格类

@Entity
public class Reseau {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int Id;
    private String Nom;
    private String Couleur;

    //I tried it with and without and it made no difference
    @OneToMany( mappedBy = "Id")
    @JsonManagedReference("id")
    private List<ReseauxSociaux> Reseaux;

    public Reseau(){}

    public Reseau(int id, String nom, String couleur) {
        Id = id;
        Nom = nom;
        Couleur = couleur;
    }
//Get Set
}

配置文件控制器

@RestController
@RequestMapping("/profil")
public class ProfilController {

    private final ProfilRepository profilRepository;

    public ProfilController(ProfilRepository profilRepository) {
        this.profilRepository = profilRepository;
    }

    @PostMapping("/getprofil/{idCompte}")
    Profil GetProfil(@PathVariable("idCompte") Long idCompte)
    {
        Profil profil= profilRepository.findProfilById(idCompte);
        return profil;
    }
}
mccptt67

mccptt671#

我终于成功了......问题的原因还不清楚,但我有两个假设:第一种是在变量名称上使用大写字母;第二种是使用在两个实体中具有相同名称的一对一的列表。
配置文件类

@Entity
@Table(name = "Profil")
public class Profil {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "idComptes", nullable = false)
    @JsonBackReference("id_comptes")
    private Comptes idComptes;
    private String avatar;
    private String banniere;
    private String pseudo;
    private String mailPro;
    private String bio;

    @ManyToMany(cascade = CascadeType.ALL,fetch = FetchType.EAGER)
    @JoinTable(name = "statut_profil", joinColumns = @JoinColumn(referencedColumnName = "id"),inverseJoinColumns = @JoinColumn(referencedColumnName ="id"))
    private List<Statut> statut;

    @OneToMany( mappedBy = "idProfil")
    @JsonManagedReference("id_profil")
    private List<ReseauxSociaux> lstprofil;

    public Profil(){}

    public Profil(Long id, Comptes idComptes, String avatar, String banniere, String pseudo, String mailPro, String bio) {
        this.id = id;
        this.idComptes = idComptes;
        this.avatar = avatar;
        this.banniere = banniere;
        this.pseudo = pseudo;
        this.mailPro = mailPro;
        this.bio = bio;
    }
//get set
}

社会研究类

@Entity
    @IdClass(ReseauxId.class)
    public class ReseauxSociaux {
        @Id
        private int id;
        @ManyToOne
        @JoinColumn(name = "id", insertable = false, updatable = false)
        @JsonBackReference("id_reseau")
        private Reseau reseau;

        @Id
        private Long idProfil;

        @ManyToOne
        @JoinColumn(name = "idProfil", insertable = false, updatable = false)
        @JsonBackReference("id_profil")
        private Profil profil;

        private String identifiant;

        private ReseauxSociaux()
        {}

        public ReseauxSociaux(int id, Reseau reseau, Long idProfil, String identifiant) {
            this.id = id;
            this.reseau = reseau;
            this.idProfil = idProfil;
            this.identifiant = identifiant;
        }
}

网格类

@Entity
public class Reseau {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    private String nom;
    private String couleur;

    @OneToMany( mappedBy = "id")
    @JsonManagedReference("id_reseau")
    private List<ReseauxSociaux> lstreseau;

    public Reseau(){}

    public Reseau(int id, String nom, String couleur) {
        this.id = id;
        this.nom = nom;
        this.couleur = couleur;
    }
}

相关问题