1 Reply Latest reply on Jun 4, 2009 5:33 AM by Jean Luc

    Effective adding data to lazy collection

    Mark Wigmans Newbie

      I have a question about how to add data to a LazyCollection. When I use the following construction to add data to the given list. 


      device.getEvents().addAll(events);


      I notice however that in this case the whole list is retrieved from the database. In my case this list of events can be huge and therefore I like to see a method to add data to the list without retrieving the whole list into memory.





      @Entity
      public class PhysicalDevice {
      ...
          @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
          @JoinColumn(name = "c_device", nullable = false)
          @IndexColumn(name = "c_nr")
          @LazyCollection(LazyCollectionOption.EXTRA)
          private List<Event> events = new ArrayList<Event>();
      ..
      }
      
      @Name("event")
      @Entity
      public class Event {
      ...
          @ManyToOne
          @JoinColumn(name = "c_device", insertable = false, updatable = false, nullable = false)
          private PhysicalDevice device;
      ...
      }
      



        • 1. Re: Effective adding data to lazy collection
          Jean Luc Apprentice

          Two options:




          • Bags (declared in Java as Collection) do not cause Hibernate to load the collection before a new element is added. If you don't care about the order of the elements, you can use them.

          • if you require the order but you don't need the entire list when you add an element, you can write a method (say, addEvent(Event e)) that issues an HQL query to write the event. You don't appear to need to have the events loaded at this time (otherwise you wouldn't have wanted to avoid that).