Announcement Announcement Module
Collapse
No announcement yet.
How to update Collection using HibernateTemplate, without previously retriving it??? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to update Collection using HibernateTemplate, without previously retriving it???

    I just cannot figure out how to perform this.... I believe it is 'catch' somewhere, but I simply could find answer neither on the google nor in the books
    Please help!
    SImply, I have an Entity who hold a collection of Objects, in relation One-To-Many unudurectional.
    The fetchType is set to LAZY. (so I do not want to retrieve 1000 objects each time, right.)
    However, NO MATTER what method I use to update collection ( I am using HibernateTemplates) at the moment when I try
    Code:
     user.getPhone().add(new Phone());
    the lazy exception has been thrown, appointing that collection isn't loaded from database. That is OK (and expected behavior because I do not want to load), I just want to UPDATE.

    I have 1000 objects in the collection, and I want to add another one.
    How to perform such a thing, without retrieving all collection data ???
    I do not believe that I must each time retrieve that collection, in order to update it ?!
    An example of solution, would be more than welcome !

    I have been wrestling with this problem for a few days ((
    Thanks in advance !

  • #2
    hi guys, this question is only related to usage of Spring's HibernateTemplates, and getting :
    Code:
    2011-06-19 01:32:26,086 ERROR org.hibernate.LazyInitializationException [LazyInitializationException.java <init> 42] --->>> could not initialize proxy - no Session 
    org.hibernate.LazyInitializationException: could not initialize proxy - no Session
    \
    If I use HibernateSession directly, I got no exceptions, if I do it in the same session.
    So, what is about HibernateTemplate issue?

    Comment


    • #3
      You need to set trasaction attribute on the method where you are performing this operation, this will ensure that the session is open.

      Comment


      • #4
        Thank you rishishehrawat. I have just worked by using directly Session from HibernateDaoSupport, but I will probably stay with your solution...Much more 'cleaner' usage...

        Comment

        Working...
        X