本文介绍了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:

如何解决这个问题?

推荐答案

一条错误消息。打印指令导致在底层。

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

类型是专门实现的,不实例化on toString()

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

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

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

另请参阅

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

09-05 02:09