gpt4 book ai didi

java - Hibernate 实体循环引用产生 StackOverflowError

转载 作者:行者123 更新时间:2023-12-01 22:35:32 37 4
gpt4 key购买 nike

问题

当尝试获取引用 ProductOrder 时,我收到 StackOverflowError ,并且 Product引用订单。我知道这是由于循环引用造成的,但我不确定如何修复它。我是 ORM 新手,但对 SQL/架构设计不熟悉:通常,我会有一个 Ordered_Products 表,它将 Order 映射到 Product ,但我认为 Hibernate 会自动为我解决这个问题。

Product 都具有唯一的 ID(这些 ID 是在应用程序外部生成的),因此没有两个 Product 是相同的。 订单 ID 也在应用程序外部生成(我将在此管道的早期流程中从 CSV 导入此数据)。

有人可以协助前进吗?

模型

订单.java

@Entity
@Table(name = "Orders")
class Order implements Serializable {

private static final long serialVersionUID = 2916987431893884948L;

@Id
@Column(name="id")
private Long id;

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

@OneToMany(mappedBy = "order", fetch=FetchType.EAGER, cascade = CascadeType.ALL)
private Set<Product> products;

// ...plain old getters and setters omitted...

public Set<Product> getProducts() {

// Should this be done at the member variable declaration?
if (null == products) {
this.products = new HashSet<Product>();
}

return products;
}

public void addProduct(Product p) {
p.setOrder(this);
getProducts().add(p);
}

public String toString() {
/* plain old to String which prints all member vars */
}
}

产品.java

@Entity
@Table(name = "Products")
public class Product implements Serializable {

private static final long serialVersionUID = 663408095532480033L;

@Id
@Column(name="id")
private String id;

@Column(name="size")
private Size size;

@OneToOne
@JoinColumn(name="order_id", nullable=false)
private Order order;

// ...plain old getters and setters...

public enum Size {
EMPTY,
XXSMALL,
XSMALL,
SMALL,
MEDIUM,
LARGE
}

public String toString() {
/* plain old to String which prints all member vars */
}
}

产生错误的代码

Product b = new Product();
b.setId("ABC123");
b.setSize(Product.Size.LARGE);

Product c = new Product();
c.setId("DEF456");
c.setSize(Product.Size.XSMALL);

Order o = new Order();
o.setId(551234L);
o.setContactName("Marco");
o.addProduct(b);
o.addProduct(c);
System.out.println("Order ID=" + o);

堆栈跟踪

Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError
at java.util.HashMap$KeyIterator.<init>(HashMap.java:1451)
at java.util.HashMap$KeySet.iterator(HashMap.java:912)
at java.util.HashSet.iterator(HashSet.java:172)
at java.util.AbstractCollection.toString(AbstractCollection.java:454)
at java.lang.String.valueOf(String.java:2979)
at java.lang.StringBuilder.append(StringBuilder.java:131)
at com.code4armour.shippingtracker.ui.model.Order.toString(Order.java:207)
at java.lang.String.valueOf(String.java:2979)
at java.lang.StringBuilder.append(StringBuilder.java:131)
at com.code4armour.shippingtracker.ui.model.Band.toString(Band.java:78)
at java.lang.String.valueOf(String.java:2979)
at java.lang.StringBuilder.append(StringBuilder.java:131)
at java.util.AbstractCollection.toString(AbstractCollection.java:462)
at java.lang.String.valueOf(String.java:2979)
at java.lang.StringBuilder.append(StringBuilder.java:131)
at com.code4armour.shippingtracker.ui.model.Order.toString(Order.java:207)
at java.lang.String.valueOf(String.java:2979)
at java.lang.StringBuilder.append(StringBuilder.java:131)
at com.code4armour.shippingtracker.ui.model.Band.toString(Band.java:78)

这里的问题是 Order.toString() 调用 Product.toString(),反之亦然

最佳答案

The problem here is the Order.toString() calling the Product.toString(), and vice versa

您已经发现了问题,很清楚为什么会出现 StackOverflowError。只是不要从另一个实体的 toString() 调用一个实体的 toString()

关于java - Hibernate 实体循环引用产生 StackOverflowError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26932387/

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