OneToMany关系无效 [英] OneToMany relationship is not working

查看:186
本文介绍了OneToMany关系无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的桌子:

产品:id,name

优惠:id,value,product_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 ()返回:

When I try to get some data from table Product, I get a java.lang.NullPointerException, and this code: product.getOffers() returns:


{IndirectSet:not instantiated}

{IndirectSet: not instantiated}

如何解决这个问题?

推荐答案

一条错误消息。打印指令导致在底层 toString() html的> IndirectSet

This not an error message. The print instruction results in toString() being invoked on the underlying IndirectSet.


当从数据库中读取包含
的域对象时,TopLink会在实例变量中放置一个IndirectSet。使用发送到IndirectSet的第一个
消息,从
数据库中获取内容并恢复正常的Set行为。

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 类型是专门实现的,不实例化on toString()

IndirectCollection types are specifically implemented not to instantiate on toString():


出于调试目的,#toString()不会触发数据库读取。

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

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

However, any other call on the indirect collection, e.g., size() or isEmpty() will instantiate the object.


当一个委托的
方法首次调用getDelegate()时,最终会触发数据库读取,而getDelegate()又调用
buildDelegate(),它将消息getValue()发送给值持有者。
值持有者执行数据库读取。使用发送到IndirectSet的第一个
消息,从
数据库中获取内容并恢复正常的Set行为。

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:未实例化

这篇关于OneToMany关系无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆