春季,JPA:列表包含值时,一对一的错误
我想在JSON中返回一个配置文件对象,其中包含与社交网络关联的登录详细信息列表。
当“ reseaux_sociaux”表为空时,一切正常。对于我的状态表,我在个人资料对象中以JSON格式获得状态。但是,当“ reseaux_sociaux”包含值时,我会在下面获得错误,并且我的个人资料对象以JSON格式返回...
(logs)
Reseau
@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;
}
}
Ociaux类
@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
}
profil控制器
@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;
}
}
I want to return a Profile Object in JSON containing a list of login details associated with a social network.
Everything works correctly when the "reseaux_sociaux" table is empty. For my status table I get my statuses in JSON format in my Profile object. However, when "reseaux_sociaux" contains values then I get the error below and my Profile object in JSON format is not returned...
(Logs)
https://cloudvyzor.com/logpad/?query&database=sandbox-7fb06b2c06f198a7c0e4ff7c74d659e0
Profil Class
@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;
}
}
ReseauxSociaux Class
@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;
}
}
Reseau class
@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
}
Profil Controller
@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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我终于成功了……问题的原因尚不清楚,但我有两个假设:第一个是在可变名称上使用大写字母;第二个是在两个实体中使用相同名称的OneTomany使用列表。
profil类
Reseauxsociaux类
Reseau类
I finally succeeded... The cause of the problem remains unclear but I have two hypotheses: the first is the use of capital letters on variable names; the second is the use of a list with the onetomany with the same name in two entities.
Profil class
ReseauxSociaux class
Reseau class