获取导致计算器错误的嵌套实体数据
Getting nested entity data resulting in stackoverflow error
我在 2 tables 用户和笑话之间存在多对多关系。它们通过连接 table fav_joke 连接起来,其中包含来自这些 table 的 id 值。根据我的设置方式,当我从这些 table 中插入和删除时,它似乎可以正常工作。
问题是当我尝试查询我的用户 table 以获取所有相关的笑话时。再次工作正常,但以某种初始形式重复它,最终出现 Whosebug 错误。请参阅图像以查看响应。如您所见,我在笑话中找到了一个用户,而笑话又有自己的笑话,笑话有自己的用户,并且一直在继续...
当我进行此查询时,我只希望用户下有相关的笑话,在这种情况下应该是 2,仅此而已。我认为这与我在 Joke Entity 中使用 @ManyToMany 关系注释有关,但我相信我需要它。请指教。谢谢。
实体
@Getter
@Setter
@NoArgsConstructor
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
long memberId;
@ManyToMany
@JoinTable(
name = "fav_joke",
joinColumns = @JoinColumn(name = "memberId"),
inverseJoinColumns = @JoinColumn(name = "id")
)
private Set<Joke> jokes;
}
@Getter
@Setter
@NoArgsConstructor
@Entity
@Table(name = "joke")
public class Joke {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String question;
private String answer;
private boolean isFav;
@ManyToMany(mappedBy = "jokes")
private Set<User> users;
}
存储库
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
User getUserByMemberId(long memberId);
}
@Repository
public interface JokeRepository extends JpaRepository<Joke, Long> {
@Query("select j from Joke j where j.id =:id")
Joke getJokeById(long id);
}
控制器 - 此调用是将堆栈抛出流的调用。
@GetMapping("/get/fav/member_id/{memberId}")
public Set<Joke> getAllFavJokes(@PathVariable long memberId){
User user = userRepository.getUserByMemberId(memberId);
return user.getJokes();
}
我认为这不相关。只是添加它以防万一。这样做会填充由于 ManyToMany 映射而动态创建的 fav_joke table。
@GetMapping("/fav/joke_id/{id}/member_id/{memberId}/isFav/{isFav}")
public String toggleFavJoke(@PathVariable long id, @PathVariable long memberId, @PathVariable boolean isFav){
Joke joke = jokeRepository.getJokeById(id);
User user = userRepository.findById(memberId).orElse(null);
if(user != null && joke != null){
if(user.getJokes() != null){
if(isFav){
user.getJokes().remove(joke);
}else {
user.getJokes().add(joke);
}
}else {
if(!isFav){
user.setJokes(Stream.of(joke).collect(Collectors.toSet()));
}
}
userRepository.save(user);
return "Fav toggled successfully";
}
return "Unable to add fav. Invalid user or joke";
}
目前table里面的信息如下:(笑话table有1k+行)
更新
按照评论中的建议添加了@@JsonIdentityInfo 注释。堆栈溢出问题现在已经消失,但信息附加的方式不正确。
这是我得到的 JSON 格式。
[{
"id": 5,
"question": "What is a gas station's favorite type of shoe?",
"answer": "Pumps.",
"users": [{
"memberId": 1,
"jokes": [5, {
"id": 7,
"question": "What kind of place should you never take a dog?",
"answer": "To the Flea Market.",
"users": [1],
"fav": false
}, {
"id": 8,
"question": "What do you call a cow in an earthquake?",
"answer": "A milkshake!",
"users": [1],
"fav": false
}, {
"id": 6,
"question": "Why was 6 afraid of 7?",
"answer": "Because 7 8 9!",
"users": [1],
"fav": false
}, {
"id": 4,
"question": "Why was Dracula put in jail?",
"answer": "He tried to rob a blood bank.",
"users": [1],
"fav": false
}]
}],
"fav": false
}, 7, 8, 6, 4]
奇怪的是,只有第一个笑话保持在顶部,其余的一直附加到用户列表中,如上图所示。
我期待以下内容(并计划排除内部用户数据)。
[
{
"id": 6,
"question": "J1 What is a gas station's favorite type of shoe?",
"answer": "Pumps.",
"users": [{
"memberId": 1,
"jokes": [5]
}],
"fav": false
},
{
"id": 6,
"question": "J2 What is a gas station's favorite type of shoe?",
"answer": "Pumps.",
"users": [{
"memberId": 1,
"jokes": [6]
}],
"fav": false
},
{
"id": 7,
"question": "J3 What is a gas station's favorite type of shoe?",
"answer": "Pumps.",
"users": [{
"memberId": 1,
"jokes": [7]
}],
"fav": false
}
]
您可以使用 @JsonIgnoreProperties
来打破循环:
public class Joke {
@JsonIgnoreProperties("jokes")
public Set<User> getUsers(){
return this.users;
}
}
如果你得到 User 可能会发生同样的问题,所以你可能还需要打破 User 的循环:
public class User {
@JsonIgnoreProperties("users")
public Set<Joke> getJokes(){
return this.jokes;
}
}
我在 2 tables 用户和笑话之间存在多对多关系。它们通过连接 table fav_joke 连接起来,其中包含来自这些 table 的 id 值。根据我的设置方式,当我从这些 table 中插入和删除时,它似乎可以正常工作。
问题是当我尝试查询我的用户 table 以获取所有相关的笑话时。再次工作正常,但以某种初始形式重复它,最终出现 Whosebug 错误。请参阅图像以查看响应。如您所见,我在笑话中找到了一个用户,而笑话又有自己的笑话,笑话有自己的用户,并且一直在继续...
当我进行此查询时,我只希望用户下有相关的笑话,在这种情况下应该是 2,仅此而已。我认为这与我在 Joke Entity 中使用 @ManyToMany 关系注释有关,但我相信我需要它。请指教。谢谢。
实体
@Getter
@Setter
@NoArgsConstructor
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
long memberId;
@ManyToMany
@JoinTable(
name = "fav_joke",
joinColumns = @JoinColumn(name = "memberId"),
inverseJoinColumns = @JoinColumn(name = "id")
)
private Set<Joke> jokes;
}
@Getter
@Setter
@NoArgsConstructor
@Entity
@Table(name = "joke")
public class Joke {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String question;
private String answer;
private boolean isFav;
@ManyToMany(mappedBy = "jokes")
private Set<User> users;
}
存储库
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
User getUserByMemberId(long memberId);
}
@Repository
public interface JokeRepository extends JpaRepository<Joke, Long> {
@Query("select j from Joke j where j.id =:id")
Joke getJokeById(long id);
}
控制器 - 此调用是将堆栈抛出流的调用。
@GetMapping("/get/fav/member_id/{memberId}")
public Set<Joke> getAllFavJokes(@PathVariable long memberId){
User user = userRepository.getUserByMemberId(memberId);
return user.getJokes();
}
我认为这不相关。只是添加它以防万一。这样做会填充由于 ManyToMany 映射而动态创建的 fav_joke table。
@GetMapping("/fav/joke_id/{id}/member_id/{memberId}/isFav/{isFav}")
public String toggleFavJoke(@PathVariable long id, @PathVariable long memberId, @PathVariable boolean isFav){
Joke joke = jokeRepository.getJokeById(id);
User user = userRepository.findById(memberId).orElse(null);
if(user != null && joke != null){
if(user.getJokes() != null){
if(isFav){
user.getJokes().remove(joke);
}else {
user.getJokes().add(joke);
}
}else {
if(!isFav){
user.setJokes(Stream.of(joke).collect(Collectors.toSet()));
}
}
userRepository.save(user);
return "Fav toggled successfully";
}
return "Unable to add fav. Invalid user or joke";
}
目前table里面的信息如下:(笑话table有1k+行)
更新
按照评论中的建议添加了@@JsonIdentityInfo 注释。堆栈溢出问题现在已经消失,但信息附加的方式不正确。
这是我得到的 JSON 格式。
[{
"id": 5,
"question": "What is a gas station's favorite type of shoe?",
"answer": "Pumps.",
"users": [{
"memberId": 1,
"jokes": [5, {
"id": 7,
"question": "What kind of place should you never take a dog?",
"answer": "To the Flea Market.",
"users": [1],
"fav": false
}, {
"id": 8,
"question": "What do you call a cow in an earthquake?",
"answer": "A milkshake!",
"users": [1],
"fav": false
}, {
"id": 6,
"question": "Why was 6 afraid of 7?",
"answer": "Because 7 8 9!",
"users": [1],
"fav": false
}, {
"id": 4,
"question": "Why was Dracula put in jail?",
"answer": "He tried to rob a blood bank.",
"users": [1],
"fav": false
}]
}],
"fav": false
}, 7, 8, 6, 4]
奇怪的是,只有第一个笑话保持在顶部,其余的一直附加到用户列表中,如上图所示。
我期待以下内容(并计划排除内部用户数据)。
[
{
"id": 6,
"question": "J1 What is a gas station's favorite type of shoe?",
"answer": "Pumps.",
"users": [{
"memberId": 1,
"jokes": [5]
}],
"fav": false
},
{
"id": 6,
"question": "J2 What is a gas station's favorite type of shoe?",
"answer": "Pumps.",
"users": [{
"memberId": 1,
"jokes": [6]
}],
"fav": false
},
{
"id": 7,
"question": "J3 What is a gas station's favorite type of shoe?",
"answer": "Pumps.",
"users": [{
"memberId": 1,
"jokes": [7]
}],
"fav": false
}
]
您可以使用 @JsonIgnoreProperties
来打破循环:
public class Joke {
@JsonIgnoreProperties("jokes")
public Set<User> getUsers(){
return this.users;
}
}
如果你得到 User 可能会发生同样的问题,所以你可能还需要打破 User 的循环:
public class User {
@JsonIgnoreProperties("users")
public Set<Joke> getJokes(){
return this.jokes;
}
}