如何正确加载一个域的数据?

tp5buhyn  于 2021-08-09  发布在  Java
关注(0)|答案(0)|浏览(125)

userprofile(一个)和goals数据(多个)。我试图返回一个简洁的json,它与第一个元素没有区别,第一个元素包含userprofile的全部数据。基本上,我要做的是返回json对象,其中包含goals数据的元素,只有一个userprofile的id字段。
这是userprofile类

@Entity                                                                                                                                    
@Table(name = "user_profiles")                                                                                                             
@JsonIdentityInfo(generator= ObjectIdGenerators.UUIDGenerator.class, property="@id")                                                       
public class UserProfile implements Serializable {  

@Id                                                                                                                                    
@GeneratedValue(generator = "uuid2")                                                                                                   
@GenericGenerator(name = "uuid2", strategy = "uuid2")                                                                                  
@Column(columnDefinition = "BINARY(16)")                                                                                               
private UUID id;                                                                                                                       

private String name;                                                                                                                   

private int active;                                                                                                                    

@Column(name = "phone_number")                                                                                                         
@Size(max = 15)                                                                                                                        
private String phoneNumber;                                                                                                            

@Column(length = 10)                                                                                                                   
private String gender;                                                                                                                 

@Temporal(TemporalType.DATE)                                                                                                           
@Column(name = "date_of_birth")                                                                                                        
private Date dateOfBirth;                                                                                                              

@Size(max = 100)                                                                                                                       
private String city;                                                                                                                   

@Size(max = 100)                                                                                                                       
private String country;                                                                                                                

@Basic(optional = false)                                                                                                               
@CreationTimestamp                                                                                                                     
@Column(name = "created_at")                                                                                                           
@Temporal(TemporalType.TIMESTAMP)                                                                                                      
private Date createdAt = new Date(); // initialize created date                                                                        

@Basic(optional = false)                                                                                                               
@CreationTimestamp                                                                                                                     
@Column(name = "updated_at")                                                                                                           
@Temporal(TemporalType.TIMESTAMP)                                                                                                      
private Date updatedAt = new Date(); // initialize created date                                                                        

@JsonIgnore                                                                                                                            
@OneToOne(fetch = FetchType.LAZY, optional = false)                                                                                    
@JoinColumn(name = "user_id", nullable = false)                                                                                        
private User user;                                                                                                                     

@OneToMany(mappedBy = "userProfile", fetch = FetchType.LAZY,                                                                           
        cascade = CascadeType.ALL)                                                                                                     
private List<Pomodoro> pomodoros;                                                                                                      

@Lob                                                                                                                                   
private byte[] picture = new byte[0];                                                                                                  

@OneToMany(mappedBy = "userProfile", fetch = FetchType.LAZY,                                                                           
        cascade = CascadeType.ALL)                                                                                                     
private List<Goal> goals;                                                                                                              

@ManyToMany(mappedBy = "userProfile", fetch = FetchType.LAZY,                                                                          
        cascade = CascadeType.ALL)                                                                                                     
private List<Goal> sub_goals;                                                                                                          

@OneToMany(mappedBy = "userProfile", fetch = FetchType.LAZY,                                                                           
        cascade = CascadeType.ALL)                                                                                                     
private List<PomodoroMusic> pomodoroMusic;                                                                                             

public UserProfile(String name, @Size(max = 15) String phoneNumber,                                                                    
                   String gender,                                                                                                      
                   Date dateOfBirth,                                                                                                   
                   @Size(max = 100) String city,                                                                                       
                   @Size(max = 100) String country,                                                                                    
                    int active                                                                                                         
){                                                                                                                                     
    this.name = name;                                                                                                                  
    this.phoneNumber = phoneNumber;                                                                                                    
    this.gender = gender;                                                                                                              
    this.dateOfBirth = dateOfBirth;                                                                                                    
    this.city = city;                                                                                                                  
    this.country = country;                                                                                                            
    this.active = active;                                                                                                              
    this.createdAt = new Date();                                                                                                       
    this.updatedAt = new Date();                                                                                                       
}      
... getters /setters
}

这是目标班

@Entity
@Table(name = "user_goals")
public class Goal {
    @Id
    @GeneratedValue(generator = "uuid2")
    @GenericGenerator(name = "uuid2", strategy = "uuid2")
    @Column(columnDefinition = "BINARY(16)")
    private UUID id;

    private String title;

    private String description;

    @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "user_profile", nullable = false)
    private UserProfile userProfile;

    @NonNull
    @Column(columnDefinition = "TEXT")
    @Convert(converter= JSONObjectConverter.class)
    private JSONObject jsonData;

    private String dailyTimePerDay;

    private String timeDone;

    private String timeDoneForTheDay;

    private String deadlineSetter;

    private boolean isPrivate;

    @ManyToMany(cascade = {
            CascadeType.PERSIST,
            CascadeType.MERGE
    })
    @JoinTable(name = "sub_goals",
            joinColumns = @JoinColumn(name = "user_profile_id"),
            inverseJoinColumns = @JoinColumn(name = "goal_id")
    )
    private List<UserProfile> attendees = new ArrayList();

    @Basic(optional = false)
    @CreationTimestamp
    @Column(name = "created_at")
    @Temporal(TemporalType.TIMESTAMP)
    private Date createdAt = new Date();

    @Basic(optional = false)
    @CreationTimestamp
    @Column(name = "updated_at")
    @Temporal(TemporalType.TIMESTAMP)
    private Date updatedAt = new Date();
...getter/setter
}

这就是我所说的React。

{
  "content": [
    {
      "id": "c4a44edc-3e0e-4087-bcc9-fc5251ceb6ce",
      "title": "Normal123123",
      "description": "asd123123",
      "userProfile": {
        "@id": "cac7fa82-dddc-4ae7-be1c-d19c99bfb8d0",
        "id": "cb9f6a3b-abbd-4a3d-877e-a6d99b1936d7",
        "name": "Test Test",
        "active": 1,
        "phoneNumber": "2025550199",
        "gender": "OTHER",
        "dateOfBirth": "2020-07-02",
        "city": "New York",
        "country": "United States",
        "createdAt": "2020-06-20T16:04:22.000+0000",
        "updatedAt": "2020-06-20T16:04:55.000+0000",
        "pomodoros": [],
        "picture": "data:image/png;base64,iVBORw0KGgoAAAANSUhE...",
        "goals": [
          {
            "id": "c4a44edc-3e0e-4087-bcc9-fc5251ceb6ce",
            "title": "Normal123123",
            "description": "asd123123",
            "userProfile": "cac7fa82-dddc-4ae7-be1c-d19c99bfb8d0",
            "jsonData": {
              "empty": false
            },
            "dailyTimePerDay": "3:30",
            "timeDone": "0",
            "timeDoneForTheDay": "0",
            "deadlineSetter": "2020-07-08",
            "attendees": [
              "cac7fa82-dddc-4ae7-be1c-d19c99bfb8d0"
            ],
            "createdAt": "2020-06-20T16:05:13.000+0000",
            "updatedAt": "2020-06-20T16:05:41.000+0000",
            "private": true
          },
          {
            "id": "08f9da9b-eaf7-402c-9fed-a8db409a2e2a",
            "title": "asdas 123",
            "description": "asd asd asd",
            "userProfile": "cac7fa82-dddc-4ae7-be1c-d19c99bfb8d0",
            "jsonData": {
              "empty": false
            },
            "dailyTimePerDay": "5:30",
            "timeDone": "0",
            "timeDoneForTheDay": "0",
            "deadlineSetter": "2020-07-09",
            "attendees": [
              "cac7fa82-dddc-4ae7-be1c-d19c99bfb8d0"
            ],
            "createdAt": "2020-06-20T16:07:23.000+0000",
            "updatedAt": "2020-06-20T16:07:23.000+0000",
            "private": true
          }
        ],
        "sub_goals": [
          {
            "id": "c4a44edc-3e0e-4087-bcc9-fc5251ceb6ce",
            "title": "Normal123123",
            "description": "asd123123",
            "userProfile": "cac7fa82-dddc-4ae7-be1c-d19c99bfb8d0",
            "jsonData": {
              "empty": false
            },
            "dailyTimePerDay": "3:30",
            "timeDone": "0",
            "timeDoneForTheDay": "0",
            "deadlineSetter": "2020-07-08",
            "attendees": [
              "cac7fa82-dddc-4ae7-be1c-d19c99bfb8d0"
            ],
            "createdAt": "2020-06-20T16:05:13.000+0000",
            "updatedAt": "2020-06-20T16:05:41.000+0000",
            "private": true
          },
          {
            "id": "08f9da9b-eaf7-402c-9fed-a8db409a2e2a",
            "title": "asdas 123",
            "description": "asd asd asd",
            "userProfile": "cac7fa82-dddc-4ae7-be1c-d19c99bfb8d0",
            "jsonData": {
              "empty": false
            },
            "dailyTimePerDay": "5:30",
            "timeDone": "0",
            "timeDoneForTheDay": "0",
            "deadlineSetter": "2020-07-09",
            "attendees": [
              "cac7fa82-dddc-4ae7-be1c-d19c99bfb8d0"
            ],
            "createdAt": "2020-06-20T16:07:23.000+0000",
            "updatedAt": "2020-06-20T16:07:23.000+0000",
            "private": true
          }
        ]
      },
      "jsonData": {
        "empty": false
      },
      "dailyTimePerDay": "3:30",
      "timeDone": "0",
      "timeDoneForTheDay": "0",
      "deadlineSetter": "2020-07-08",
      "attendees": [
        "cac7fa82-dddc-4ae7-be1c-d19c99bfb8d0"
      ],
      "createdAt": "2020-06-20T16:05:13.000+0000",
      "updatedAt": "2020-06-20T16:05:41.000+0000",
      "private": true
    },
    {
      "id": "08f9da9b-eaf7-402c-9fed-a8db409a2e2a",
      "title": "asdas 123",
      "description": "asd asd asd",
      "userProfile": "cac7fa82-dddc-4ae7-be1c-d19c99bfb8d0",
      "jsonData": {
        "empty": false
      },
      "dailyTimePerDay": "5:30",
      "timeDone": "0",
      "timeDoneForTheDay": "0",
      "deadlineSetter": "2020-07-09",
      "attendees": [
        "cac7fa82-dddc-4ae7-be1c-d19c99bfb8d0"
      ],
      "createdAt": "2020-06-20T16:07:23.000+0000",
      "updatedAt": "2020-06-20T16:07:23.000+0000",
      "private": true
    }
  ],
  "pageable": {
    "sort": {
      "sorted": false,
      "unsorted": true,
      "empty": true
    },
    "offset": 0,
    "pageNumber": 0,
    "pageSize": 10,
    "paged": true,
    "unpaged": false
  },
  "totalPages": 1,
  "totalElements": 2,
  "last": true,
  "number": 0,
  "sort": {
    "sorted": false,
    "unsorted": true,
    "empty": true
  },
  "size": 10,
  "first": true,
  "numberOfElements": 2,
  "empty": false
}ted": true,
    "empty": true
  },
  "size": 10,
  "first": true,
  "numberOfElements": 2,
  "empty": false
}

我是怎么想的

{
  "content": [
    {
      "id": "c4a44edc-3e0e-4087-bcc9-fc5251ceb6ce",
      "title": "Normal123123",
      "description": "asd123123",
      "userProfile": "cac7fa82-dddc-4ae7-be1c-d19c99bfb8d0",
      "jsonData": {
        "empty": false
      },
      "dailyTimePerDay": "3:30",
      "timeDone": "0",
      "timeDoneForTheDay": "0",
      "deadlineSetter": "2020-07-08",
      "attendees": [
        "cac7fa82-dddc-4ae7-be1c-d19c99bfb8d0"
      ],
      "createdAt": "2020-06-20T16:05:13.000+0000",
      "updatedAt": "2020-06-20T16:05:41.000+0000",
      "private": true
    },
    {
      "id": "08f9da9b-eaf7-402c-9fed-a8db409a2e2a",
      "title": "asdas 123",
      "description": "asd asd asd",
      "userProfile": "cac7fa82-dddc-4ae7-be1c-d19c99bfb8d0",
      "jsonData": {
        "empty": false
      },
      "dailyTimePerDay": "5:30",
      "timeDone": "0",
      "timeDoneForTheDay": "0",
      "deadlineSetter": "2020-07-09",
      "attendees": [
        "cac7fa82-dddc-4ae7-be1c-d19c99bfb8d0"
      ],
      "createdAt": "2020-06-20T16:07:23.000+0000",
      "updatedAt": "2020-06-20T16:07:23.000+0000",
      "private": true
    }
  ],
  "pageable": {
    "sort": {
      "sorted": false,
      "unsorted": true,
      "empty": true
    },
    "offset": 0,
    "pageNumber": 0,
    "pageSize": 10,
    "paged": true,
    "unpaged": false
  },
  "totalPages": 1,
  "totalElements": 2,
  "last": true,
  "number": 0,
  "sort": {
    "sorted": false,
    "unsorted": true,
    "empty": true
  },
  "size": 10,
  "first": true,
  "numberOfElements": 2,
  "empty": false
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题