совместная таблица @onetomany и @manytoone при получении всех данных снова и снова повторяет одну и ту же родительскую таблицу SpringBoot

#java #hibernate #jpa #entity

#java #впадать в спящий режим #jpa #сущность

Вопрос:

совместная таблица @onetomany и @manytoone при получении всех данных повторяет одну и ту же родительскую таблицу снова и снова

это мой класс @Entity

     package com.social.webapp.entity;


import java.io.Serializable;
import java.sql.Date;
import java.sql.Time;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

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.OneToMany;
import javax.persistence.Table;

import javax.validation.constraints.NotNull;



import com.sun.istack.Nullable;



@Entity
@Table(name = "userdetails")
public class UserDetails  implements Serializable{
    
    private static final long serialVersionUID = -7302800336276816169L;
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
     long userid;
    @Column(name = "mobilenumber",nullable = false)
    @NotNull
     String mobilenumber;
    @Column(name = "email",nullable =false )
    @Nullable
     String email;
    @Column(name = "profilename",nullable = true)
    @Nullable
     String profilename;
    @Column(name = "profileimage",nullable = true)
    @Nullable
     String profileimage;
    @Column(name = "firebaseuid",nullable = false)
    @NotNull
     String firebaseuid;
    
    @Column(name = "createddate",nullable = false)
    @NotNull
     Date createddate;
    
    
    @Column(name = "createdtime",nullable = false)
    @NotNull
     Time createdtime;
    @Column(name = "delete",nullable = true)
    @Nullable
    int delete;
    
    @OneToMany(targetEntity = PostDetails.class,fetch = FetchType.LAZY,mappedBy = "userdetails",cascade = CascadeType.ALL)
    public List<PostDetails> postlist = new ArrayList<PostDetails>();

    public UserDetails() {
        
        // TODO Auto-generated constructor stub
        
    }
    public UserDetails(@NotNull String mobilenumber, String email, String profilename, String profileimage,
            @NotNull String firebaseuid, @NotNull Date createddate, @NotNull Time createdtime, int delete) {
        
        this.mobilenumber = mobilenumber;
        this.email = email;
        this.profilename = profilename;
        this.profileimage = profileimage;
        this.firebaseuid = firebaseuid;
        this.createddate = createddate;
        this.createdtime = createdtime;
        this.delete = delete;
    }

    public long getUserid() {
        return userid;
    }
    public void setUserid(long userid) {
        this.userid = userid;
    }
    public String getMobilenumber() {
        return mobilenumber;
    }
    public void setMobilenumber(String mobilenumber) {
        this.mobilenumber = mobilenumber;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getProfilename() {
        return profilename;
    }
    public void setProfilename(String profilename) {
        this.profilename = profilename;
    }
    public String getProfileimage() {
        return profileimage;
    }
    public void setProfileimage(String profileimage) {
        this.profileimage = profileimage;
    }
    public String getFirebaseuid() {
        return firebaseuid;
    }
    public void setFirebaseuid(String firebaseuid) {
        this.firebaseuid = firebaseuid;
    }
    public Date getCreateddate() {
        return createddate;
    }
    public void setCreateddate(Date createddate) {
        this.createddate = createddate;
    }
    public Time getCreatedtime() {
        return createdtime;
    }
    public void setCreatedtime(Time createdtime) {
        this.createdtime = createdtime;
    }
    public int getDelete() {
        return delete;
    }
    public void setDelete(int delete) {
        this.delete = delete;
    }
//  public java.util.List<PostDetails> getPostlist() {
//      return postlist;
//  }
//  public void setPostlist(java.util.List<PostDetails> postlist) {
//      this.postlist = postlist;
//  }


}
  

это другой @Entity

 package com.social.webapp.entity;

import java.io.Serializable;
import java.sql.Date;
import java.sql.Time;

import javax.persistence.Basic;
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.JoinTable;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.sun.istack.Nullable;

@Entity
@Table(name = "postdetails")
//@JsonInclude(value = Include.NON_NULL)
//@JsonIgnoreProperties(ignoreUnknown = true)
public class PostDetails implements Serializable{
    private static final long serialVersionUID = -7302800336276816169L;
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long postid;
    
    @ManyToOne
    @JoinColumn(name = "userdetail_id")
    @JsonIgnoreProperties(ignoreUnknown = true)
    public UserDetails userdetails;
    
    @Column(name = "message")
    @Nullable
    private String message;
    
    @Column(name = "likecount",nullable = true)
    private String likescount;
    
    @Column(name = "sharecount",nullable = true)
    private String sharecount;
    
    @Column(name = "postdate",nullable = true)
    private Date postdate;
    
    @Column(name = "posttime",nullable = true)
    private Time posttime;
    
    @Column(name = "deletedate",nullable = true)
    private Date deletedate;
    
    @Column(name = "deletetime",nullable = true)
    private Time deletetime;
    
    @Column(name = "delete",nullable = true)
    private int delete;
    
    @Lob @Basic(fetch = FetchType.LAZY)
    @Column(name = "filedata",nullable = true,length = 1000000000)
    private byte[] filedata;
    
    @Column(name = "originalname",nullable = true)
    private String originalname;
    
    @Column(name = "filetype",nullable=true)
    private String filetype;
    
    @Column (name = "filesize",nullable=true)
    private long filesize;

    public PostDetails( String message, Date postdate, Time posttime, byte[] filedata,
            String originalname, String filetype, long filesize) {
        super();
//      this.userdetailid=userdetailid;
        this.message = message;
        this.postdate = postdate;
        this.posttime = posttime;
        this.filedata = filedata;
        this.originalname = originalname;
        this.filetype = filetype;
        this.filesize = filesize;
    }

    public PostDetails() {
        
        // TODO Auto-generated constructor stub
    }

    public UserDetails getUserdetailid() {
        return userdetails;
    }

    public void setUserdetailid(UserDetails userdetailid) {
        this.userdetails = userdetailid;
    }
    
    
    
    
}
  

когда я получаю все данные из класса userdetails, я не получаю класс postdetails в списке, вместо этого я получаю класс userdetails в цикле…

 [
    {
        "userid": 1,
        "mobilenumber": "null",
        "email": "lr5122@srmist.edu.in",
        "profilename": "Lawrance Raj (PA1912005020001)",
        "profileimage": "https://lh6.googleusercontent.com/-ZokXcLoflks/AAAAAAAAAAI/AAAAAAAAAAA/AMZuucnDyqHvXP3sA_7POR3ejVvw2KM-4A/photo.jpg",
        "firebaseuid": "GtPkv3DbGhXct30HYA5VprxeWDm1",
        "createddate": "2020-09-02",
        "createdtime": "19:47:02",
        "delete": 1,
        "postlist": [
            {
                "userdetails": {
                    "userid": 1,
                    "mobilenumber": "null",
                    "email": "lr5122@srmist.edu.in",
                    "profilename": "Lawrance Raj (PA1912005020001)",
                    "profileimage": "https://lh6.googleusercontent.com/-ZokXcLoflks/AAAAAAAAAAI/AAAAAAAAAAA/AMZuucnDyqHvXP3sA_7POR3ejVvw2KM-4A/photo.jpg",
                    "firebaseuid": "GtPkv3DbGhXct30HYA5VprxeWDm1",
                    "createddate": "2020-09-02",
                    "createdtime": "19:47:02",
                    "delete": 1,
                    "postlist": [
                        {
                            "userdetails": {
                                "userid": 1,
                                "mobilenumber": "null",
                                "email": "lr5122@srmist.edu.in",
                                "profilename": "Lawrance Raj (PA1912005020001)",
                                "profileimage": "https://lh6.googleusercontent.com/-ZokXcLoflks/AAAAAAAAAAI/AAAAAAAAAAA/AMZuucnDyqHvXP3sA_7POR3ejVvw2KM-4A/photo.jpg",
                                "firebaseuid": "GtPkv3DbGhXct30HYA5VprxeWDm1",
                                "createddate": "2020-09-02",
                                "createdtime": "19:47:02",
                                "delete": 1,
                                "postlist": [
                                    {
                                        "userdetails": {
                                            "userid": 1,
                                            "mobilenumber": "null",
                                            "email": "lr5122@srmist.edu.in",
                                            "profilename": "Lawrance Raj (PA1912005020001)",
                                            "profileimage": "https://lh6.googleusercontent.com/-ZokXcLoflks/AAAAAAAAAAI/AAAAAAAAAAA/AMZuucnDyqHvXP3sA_7POR3ejVvw2KM-4A/photo.jpg",
                                            "firebaseuid": "GtPkv3DbGhXct30HYA5VprxeWDm1",
                                            "createddate": "2020-09-02",
                                            "createdtime": "19:47:02",
                                            "delete": 1,
                                            "postlist": [
                                                {
                                                    "userdetails": {
                                                        "userid": 1,
                                                        "mobilenumber": "null",
                                                        "email": "lr5122@srmist.edu.in",
                                                        "profilename": "Lawrance Raj (PA1912005020001)",
                                                        "profileimage": "https://lh6.googleusercontent.com/-ZokXcLoflks/AAAAAAAAAAI/AAAAAAAAAAA/AMZuucnDyqHvXP3sA_7POR3ejVvw2KM-4A/photo.jpg",
                                                        "firebaseuid": "GtPkv3DbGhXct30HYA5VprxeWDm1",
                                                        "createddate": "2020-09-02",
                                                        "createdtime": "19:47:02",
                                                        "delete": 1,
                                                        "postlist": [
                                                            {
                                                                "userdetails": {
                                                                    "userid": 1,
                                                                    "mobilenumber": "null",
                                                                    "email": "lr5122@srmist.edu.in",
                                                                    "profilename": "Lawrance Raj (PA1912005020001)",
                                                                    "profileimage": "https://lh6.googleusercontent.com/-ZokXcLoflks/AAAAAAAAAAI/AAAAAAAAAAA/AMZuucnDyqHvXP3sA_7POR3ejVvw2KM-4A/photo.jpg",
                                                                    "firebaseuid": "GtPkv3DbGhXct30HYA5VprxeWDm1",
                                                                    "createddate": "2020-09-02",
                                                                    "createdtime": "19:47:02",
                                                                    "delete": 1,
                                                                    "postlist": [
                                                                        {
                                                                            "userdetails": {
                                                                                "userid": 1,
                                                                                "mobilenumber": "null",
                                                                                "email": "lr5122@srmist.edu.in",
                                                                                "profilename": "Lawrance Raj (PA1912005020001)",
                                                                                "profileimage": "https://lh6.googleusercontent.com/-ZokXcLoflks/AAAAAAAAAAI/AAAAAAAAAAA/AMZuucnDyqHvXP3sA_7POR3ejVvw2KM-4A/photo.jpg",
                                                                                "firebaseuid": "GtPkv3DbGhXct30HYA5VprxeWDm1",
                                                                                "createddate": "2020-09-02",
                                                                                "createdtime": "19:47:02",
                                                                                "delete": 1,
                                                                                "postlist": [
                                                                                    {
                                                                                        "userdetails": {
                                                                                            "userid": 1,
                                                                                            "mobilenumber": "null",
                                                                                            "email": "lr5122@srmist.edu.in",
                                                                                            "profilename": "Lawrance Raj (PA1912005020001)",
                                                                                            "profileimage": "https://lh6.googleusercontent.com/-ZokXcLoflks/AAAAAAAAAAI/AAAAAAAAAAA/AMZuucnDyqHvXP3sA_7POR3ejVvw2KM-4A/photo.jpg",
                                                                                            "firebaseuid": "GtPkv3DbGhXct30HYA5VprxeWDm1",
                                                                                            "createddate": "2020-09-02",
                                                                                            "createdtime": "19:47:02",
                                                                                            "delete": 1,
                                                                                            "postlist": [
                                                                                                {
                                                                                                    "userdetails": {
                                                                                                        "userid": 1,
                                                                                                        "mobilenumber": "null",
                                                                                                        "email": "lr5122@srmist.edu.in",
                                                                                                        "profilename": "Lawrance Raj (PA1912005020001)",
                                                                                                        "profileimage": "https://lh6.googleusercontent.com/-ZokXcLoflks/AAAAAAAAAAI/AAAAAAAAAAA/AMZuucnDyqHvXP3sA_7POR3ejVvw2KM-4A/photo.jpg",
                                                                                                        "firebaseuid": "GtPkv3DbGhXct30HYA5VprxeWDm1",
                                                                                                        "createddate": "2020-09-02",
                                                                                                        "createdtime": "19:47:02",
                                                                                                        "delete": 1,
                                                                                                        "postlist": [
                                                                                                            {
                                                                                                                "userdetails": {
                                                                                                                    "userid": 1,
                                                                                                                    "mobilenumber": "null",
                                                                                                                    "email": "lr5122@srmist.edu.in",
                                                                                                                    "profilename": "Lawrance 
  

как будто это начинается как большой инфинитивный цикл, я не знаю, почему я перепробовал все, что было, я не могу найти решения .. кто-нибудь, помогите мне найти решения…

Ответ №1:

Решение:

Используйте

аннотация @JsonManagedReference для первых созданных объектов

аннотация @JsonBackReference для вторых созданных объектов