一对多关系不起作用
我的表:
产品:id、名称
报价:id、值、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;
...
}
当我尝试从表产品
获取一些数据时,我得到一个 java.lang.NullPointerException ,并且此代码:product.getOffers() 返回:
{IndirectSet:未实例化}
如何解决这个问题?
My Tables:
Product: id, name
Offer: id, value, product_id
Entities:
@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;
...
}
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}
How to fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这不是错误消息。 print 指令导致在底层 间接设置。
IndirectCollection 类型专门实现为不实例化 toString():
但是,对间接集合的任何其他调用(例如 size() 或 isEmpty())都将实例化该对象。
另请参阅IndirectList:未实例化
This not an error message. The print instruction results in toString() being invoked on the underlying IndirectSet.
IndirectCollection types are specifically implemented not to instantiate on toString():
However, any other call on the indirect collection, e.g., size() or isEmpty() will instantiate the object.
See also IndirectList: not instantiated
如果您在访问
product.getOffers()
时收到{IndirectSet: not instantied}
,那么您很可能是在事务之外执行此代码。默认情况下,
@OneToMany
和@ManyToMany
关系为 延迟加载 这意味着,为了获得更好的性能,只有在您第一次访问数据时才会获取数据。这必须发生在活动事务中。如果您不在此范围内访问此数据,则您将无法再访问此数据。您应该将调用代码放入活动事务中,或者将集合更改为 渴望而不是懒惰:
If you get
{IndirectSet: not instantiated}
when accessingproduct.getOffers()
than most probably you're executing this code outside of the transaction.By default
@OneToMany
and@ManyToMany
relationships are lazy loaded which means that, for better performance, you'll get data fetched only when you want to access it for the first time. This must happen within an active transaction.If you don't access this data within this scope than you cannot access this data no more. You should either put your invocation code within the active transaction or change the collection to be eager instead of lazy:
这就是我所做的
Here's what I did
这解决了我的问题
@OneToMany(mappedBy = "columnName",cascade = { CascadeType.ALL}, fetch=FetchType.EAGER)
This solved my issue
@OneToMany(mappedBy = "columnName", cascade = { CascadeType.ALL}, fetch=FetchType.EAGER)
我使用 Eclipselink 2.5.2 版本作为我的 ORM 供应商,并且在延迟加载包含实体时在一对多 JPA 映射中遇到了这个问题。对我有用的解决方法是: -
从容器实体端的映射是:
从包含实体端的映射是:
I am using Eclipselink 2.5.2 version as my ORM vendor and I have faced this issue in one-to-many JPA mapping while lazy loading the containing entities. The workaround that is working for me is:-
The Mapping from Container Entity side is:
Mapping from Containing Entity side is: