gpt4 book ai didi

java - OneToMany 关系不起作用

转载 作者:IT老高 更新时间:2023-10-28 20:50:23 25 4
gpt4 key购买 nike

我的表:

产品:ID、名称

Offer:id、value、product_id

实体:

@Entity
@Table(name="product")
public class Product implements Serializable {
@OneToMany(mappedBy="product")
private Set<Offer> offers;
...
}

@Entity
@Table(name="offer")
public class Offer implements Serializable {
@ManyToOne
@JoinColumn(name="PRODUCT_ID")
private Product product;
...
}

当我尝试从表 Product 中获取一些数据时,我得到一个 java.lang.NullPointerException,并且这个代码:product.getOffers() 返回:

{IndirectSet: not instantiated}

如何解决这个问题?

最佳答案

不是错误消息。打印指令导致在底层 IndirectSet 上调用 toString() .

TopLink will place an IndirectSet in the instance variable when the containing domain object is read from the datatabase. With the first message sent to the IndirectSet, the contents are fetched from the database and normal Set behavior is resumed.

IndirectCollection类型被具体实现为不在 toString() 上实例化:

For debugging purposes, #toString() will not trigger a database read.

但是,对间接集合的任何其他调用,例如 size() 或 isEmpty() 都会实例化该对象。

The database read is ultimately triggered when one of the "delegated" methods makes the first call to getDelegate(), which in turn calls buildDelegate(), which sends the message getValue() to the value holder. The value holder performs the database read. With the first message sent to the IndirectSet, the contents are fetched from the database and normal Set behavior is resumed.

另见 IndirectList: not instantiated

关于java - OneToMany 关系不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8301820/

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