Announcement Announcement Module
Collapse
No announcement yet.
LazyInitializationException + JPA + Hibernate Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • LazyInitializationException + JPA + Hibernate

    Hi People,

    I am using OpenEntityManagerInViewFilter for single web requests where when I query the database, I can pull data from a domain object plus any collections in that domain object per request (hence avoiding lazy loading exceptions)

    Here is the issue:
    E.g. I have a User object I query for in my web request. It has some collections, like a list of Attributes. I can access Attributes during the same web request which is fine (which is what OpenEntityManagerInViewFilter is useful for)
    What I do is save the User object in HTTPSession.
    Later on, in some other web page in some other request, I access the User object. When I access any collections of User, I get the infamous LazyInitializationException.

    This is a very specific question. How do I access any collections of User at a later time outside of the initial hibernation session (openEntityManagerInView session)??

    I have read tons of articles but still haven't bin successful in finding the answer.
    Some say use
    1) session.lock(user,LockMode.NONE);
    2) entityManager.merge(user);
    3) somehow re-attach old session to new session?
    4) use new session for existing User object

    None of these work and keep in mind I am not working directly with Hibernate Session but with JPA entityManager.

    Again, I want to access my domain object's collections in HTTPSession outside of the initial request when I first saved it. There has to be a clean solution to this.

    I hope there is a simple answer to this because its pretty frustrating.

  • #2
    Anyone out there??

    Hey People, can someone give me a hand please?

    I would like to re-access a domain object collection property (like an attributes list of a User object) that was initially read from the database that is now stored in HTTPSession.

    I do not want to store the ID of the User (db ID) in HTTPSession and then do a database call each time I need that user object. I would like to use the User object at will in different requests for the life of the HTTPSession and access any collection of the User Object.

    How can I do this? Someone give me hand, as I said I am using OpenEntityManagerInViewFilter.

    Note: I am trying to access a collection beyond the first request.

    Please advise.

    Comment


    • #3
      You would need to merge the User object stored in the HttpSession to the current Hibernate session before trying to instantiate associations. Of course this merge will have to cascade to the associations. Perhaps the merge is not cascading?

      The other option might be to eager load the associations?

      Comment


      • #4
        Merging - solved

        Yes I did this to get it to work:
        Code:
        user = entityManager.merge(user);
        I was able to access my collections now. The think I do not understand is what is meant by Cascading?

        I did find this example but am not sure what it means:
        Code:
        @Entity
        public class Employee {
            @Id private int id;
            private String name;
            private long salary;
            @ManyToOne(cascade=CascadeType.MERGE)
            private Address address;
            @ManyToOne
            private Department department;
            @OneToMany(mappedBy="employee", cascade=CascadeType.MERGE)
            private Collection<Phone> phones;
            // ...
        }

        Comment

        Working...
        X