gpt4 book ai didi

java - 无法计算表达式方法抛出 'java.lang.StackOverflowError' 异常。实体之间有嵌套关系

转载 作者:行者123 更新时间:2023-12-02 01:42:31 24 4
gpt4 key购买 nike

有一天,我正在与一个问题作斗争。让我们开始吧。我有 3 个类别,例如标签、优惠和动物

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "animals")
public class Animal {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "animal_id")
private Long id;

@Column(name = "name")
@NotNull
private String name;

@Column(name = "description")
@NotNull
private String text;

@Column(name = "quantity")
@NotNull
private int count;


@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "user_id", nullable = false)
private User user;

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "tag_id", nullable = false)
private Tag tags;


@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "offer_id", nullable = true)
private Offer offer;

优惠

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "offer")
public class Offer {


@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "offer_id")
private Long id;

@Column(name = "title")
@NotNull
private String title;

@Column(name = "description")
@NotNull
private String text;


@Column(name = "price")
@NotNull
private int price;

@Column(name = "contact")
@NotNull
private int contact;

@Temporal(TemporalType.TIMESTAMP)
@Column(name = "create_date", nullable = false, updatable = false)
@CreationTimestamp
private java.util.Date createDate;



@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "user_id", nullable = false)
private User user;


@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "tag_id", nullable = false)
private Tag tags;


@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "animal_id", nullable = false)
private Animal animal;

还有标签。

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "tags")
public class Tag {


@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "tag_id")
private long id;

@Column(name = "name")
@Email(message = "*Please provide a valid tag")
@NotEmpty(message = "*Please provide an tag")
private String name;


@OneToMany(mappedBy = "tags")
private Set<Offer> offers;

@OneToMany(mappedBy = "tags")
private Set<Animal> animals;

OneToOne 关系是因为我想为一种动物提供一个报价。

我还有 Controller ,如所示添加新报价。

@RequestMapping(value = "/postForm", method = RequestMethod.GET)
public String newOffer(Principal principal,Model model) {

String e_mail = principal.getName();
System.out.println(e_mail);
User user = userService.findUserByEmail(e_mail);

Offer offer = new Offer();
offer.setUser(user);
Long id = user.getId();

List<Tag> tags = tagService.findAlltags();


List<Animal> animal = animalRepository.findAnimalByUser(user);

model.addAttribute("tags", tags);
model.addAttribute("animals",animal);
model.addAttribute("offer", offer);

return "/postForm";
}

我正在传递诸如动物之类的列表,以及要选择标签的标签以及分配给用户的动物。我的动物库。

public interface AnimalRepository extends JpaRepository<Animal, Long> {


List<Animal> findAnimalByUser(User user);


}

实际上我在正确展示动物方面遇到了问题。标签显示正确,但动物列表无法评估动物类内的标签。更具体地说,让我向您展示 Inteliji 的回溯屏幕

imgur 链接 enter link description here也许关系出了问题,但到底是哪里出了问题?感谢您的所有回答。 :)

最佳答案

Animal 类中的

toString() 方法会导致此问题。方法从 AnimalTag 类交替构建返回字符串,这两个类都相互引用,从而产生无限过程。

摆脱@Data注释(其中包括@ToString注释),并自行实现toString()方法。

关于java - 无法计算表达式方法抛出 'java.lang.StackOverflowError' 异常。实体之间有嵌套关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54254237/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com