How to add an item to a lazily loaded collection in Hibernate without causing the collection to load?

What is said on tin; I want to change the collection in Hibernate without forcing the collection to load, since this is a large amount of data (~ 100,000 records, monotonously increasing).

I am currently adding an item to this list, invoking getEvents ().add (newEvent)which, of course, causes padding events.

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 can I do it?

+5
source share
2 answers

( Parent) , .

Hibernate :

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

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

:

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

, . .

+4

.

:

@Entity
public Document
{
    @ManyToOne
    Set<Event> events;

    public void addEvent( Event event )
    {
        events.add( event )
    }

}


@Entity
public Event
{
    @id
    private long id;

    @OneToMany
    private Document  doc;

    ....
}

, :

document.add( event );
update( document );

, :

    event = new Event();
    event.setDocument( document );
    insert( event );

, document.getEvents(), . , , 2. getEvents() , .

+1

All Articles