@onetomany and @manytoone joint table while geting all the datas repeating the same parent table again and again SpringBoot

huangapple go评论69阅读模式
英文:

@onetomany and @manytoone joint table while geting all the datas repeating the same parent table again and again SpringBoot

问题

[
    {
        "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": [...]
                                        },
                                        ...
                                    },
                                    ...
                                ]
                            },
                            ...
                        },
                        ...
                    ]
                },
                ...
            },
            ...
        ]
    },
    ...
]

Note: The JSON structure provided is a simplified representation of your original data with repetitive nesting. If you need further assistance or solutions to your issue, please provide more context or specify the problem you are facing.

英文:

@onetomany and @manytoone joint table while geting all the datas repeating the same parent table again and again

this is my @Entity Class

    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;
//	}
}

this is another @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;
}
}

when i'm geting all the datas from the userdetails class i'm not getting the postdetails class in the list insteard of i'm getting the userdetails class in a loop ...

[
{
"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 

like this its comming as al big infinitive loop i don't know why i tried all the was i can't find the solutions ..any one help me to find the solutions ...

答案1

得分: 1

解决方案:

使用@JsonManagedReference注解来标记第一个实例化的对象。

使用@JsonBackReference注解来标记第二个实例化的对象。

英文:

Solution:

Use

@JsonManagedReference annotation for the first objects instantiated

@JsonBackReference annotation for the second objects instantiated

huangapple
  • 本文由 发表于 2020年9月2日 22:40:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/63707943.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定