Results 1 to 10 of 10

Thread: Master-/Detail-Form raises exception

  1. #1
    Join Date
    Jun 2007
    Posts
    6

    Default Master-/Detail-Form raises exception

    Hello

    I am working on a billing-managing-project. A billing contains multiple invoice line items.
    The data is saved in a database and read with hibernate and spring.
    I have tried to create a master-/detail-form on which the user can edit the several invoice line items.

    But when I try to open this dialog the following error will be raised:
    org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: ch.cadi.inic.saemy.cannonball.business.Billing.ite ms, no session or session was closed

    What do I have to do to eliminate this error?

  2. #2
    Join Date
    Aug 2005
    Location
    London (the English one!)
    Posts
    378

    Default Hibernate exceptions

    HI

    I believe that you have detached objects correct?

    One way to get this fixed is to extends the AbstractTableMasterForm an add a method like this:

    Code:
        @Override
        protected Class getMasterCollectionType(final ValueModel collectionPropertyVM) {
            return Set.class;
        }
    where you return the collection type that you are using for your property: Set or List.class

    This should fix your issue, I think!

    Good luck

    Benoit

  3. #3
    Join Date
    Jun 2007
    Posts
    6

    Default

    Hi, thanks for your reply.

    Yes, I have detached objects.

    But I do already extend the AbstractTableMasterForm and override this method. (In fact my property is an org.hibernate.collection.PersistentSet)

    But the error still occurs...

  4. #4
    Join Date
    Aug 2005
    Location
    London (the English one!)
    Posts
    378

    Default

    Try Set.class EVEN if you have a persistent Set.

    Do NOT use the persistent classes.

    Let me know

    B

  5. #5
    Join Date
    Jun 2007
    Posts
    6

    Default

    I've tried both and it isn't working with any of this two methods...

  6. #6
    Join Date
    Jan 2006
    Location
    Vilnius, Lithuania
    Posts
    68

    Default

    Hi,

    I think this problem is not directly related to Spring RCP, but rather a good old Hibernate detached objects issue.

    If you are sending your Billing object through a remoting protocol, like Spring HTTP Invoker or Java RMI, then you need to make sure that your items collection, and probably all item object inside are initialized, that is loaded from the database. For instance, you can call Hibernate.initialize(myObject) to make sure your object proxy gets populated. You need to run this for every item in the collection.

    Alternatively, if your application uses Hibernate directly, you could also keep your Hibernate session open until you finish editing your billing object.

    hope this helps,

    Andrius

  7. #7
    Join Date
    Jun 2007
    Posts
    6

    Default

    I connect directly to hibernate (all runs on one pc).

    My idea is, that the list of all billings is loaded at the beginning of the program. when the user double-clicks on a billing, a dialog opens with more detailled informations - the master-/detailform.
    my hibernate connection is closed long time ago (i dont know anything about the sessionhandling, since i use spring (without rcp ))

    is there no other possibility to solve this problem. (am i the only one with this problem?)

  8. #8
    Join Date
    Jan 2006
    Location
    Vilnius, Lithuania
    Posts
    68

    Default

    You need to have at least some basic understanding how Spring handles your Hibernate sessions, because you need to make sure that all collections and entity proxies you will be needing are initialized *before* the session is closed. After the session is gone, your object becomes detached, no further data can be loaded automatically, and you get a LazyInitializationException when you request it.

    As I mentioned earlier, you can alternatively have the session open, or reattach the object to another session when the data is needed, although this is more complicated. In any case, this is a Hibernate-specific issue, not Spring RCP.

  9. #9
    Join Date
    Jun 2007
    Posts
    6

    Default

    I think I do have some basic understanding how spring works.
    What i wanted to say is that i don't know exactly when the sessions are closed and opened and I don't know how to access them.

    How can I keep the session opened?

    Or where is the right place to reattach the object to the session? In the DAOs? Because I don't like the idea reattaching the object in the view-layer...
    How do I do that?

  10. #10
    Join Date
    Jan 2006
    Location
    Vilnius, Lithuania
    Posts
    68

    Default

    Please read these docs, they should give you the necessary understanding:
    http://static.springframework.org/sp...#orm-hibernate
    http://www.hibernate.org/hib_docs/v3...nsactions.html
    http://www.hibernate.org/hib_docs/v3...state-detached

    Afterwards, make sure that your objects are properly initialized before returning them outside of transactional code (see my previous posts).

    good luck

    Andrius

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •