如何在Hibernate中将元素添加到延迟加载的集合而不会导致集合加载? [英] How can I add an element to a lazily-loaded collection in Hibernate without causing the collection to load?

查看:110
本文介绍了如何在Hibernate中将元素添加到延迟加载的集合而不会导致集合加载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它在锡上说的是什么;我想修改Hibernate中的一个集合而不强制加载集合,因为它是一个大量的数据(大约100,000条记录,单调递增)。

What it says on the tin; I want to modify a collection in Hibernate without forcing the collection to load, since it is a large volume of data (~100,000 records, monotonically increasing).

现在,我通过调用 getEvents().add(newEvent)来添加一个元素到这个列表中,这当然会导致 events

Right now, I add an element to this list by calling getEvents ().add (newEvent) which, of course, causes events to be populated.

以下是贴图:

Here's the mapping:

<bag name = "events" inverse = "true" cascade = "all-delete-orphan"
 order-by = "event_date desc" lazy = "true">
  <key>
<column name = "document_id" length = "64" not-null = "true" />
  </key>
  <one-to-many class = "EventValue" />
</bag>

我应该怎么做?

How should I be doing this?

推荐答案

完成此操作的一种方法是在父对象(让我们将其称为Parent)和事件之间创建双向关联,并配置您的hibernate映射,以使关系由Event管理。

One way to accomplish this would be to create a bidirectional association between the parent object (let's call it Parent) and the Event and configure your hibernate mappings such that the relationship is managed by Event.

为了达到这个目的,你的Hibernate映射看起来像这样:

To accomplish this, your Hibernate mappings would look something like:

<class name="Parent"...>
    ...
    <bag name="events" lazy="true" inverse="true"...>...</bag>
    ...
</class>

<class name="Event"...> 
    <many-to-one name="parent">
    ...
</class>

您的代码看起来像这样:

And your code would look something:

myEvent.setParent(parentObject);
eventDao.save(myEvent);

希望这会有所帮助。祝你好运。

Hope this helps. Good luck.

这篇关于如何在Hibernate中将元素添加到延迟加载的集合而不会导致集合加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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