Results 1 to 4 of 4

Thread: LazyInitializationException + JPA + Hibernate

  1. #1
    Join Date
    Jun 2009
    Posts
    106

    Angry 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. #2
    Join Date
    Jun 2009
    Posts
    106

    Exclamation 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.

  3. #3
    Join Date
    Jun 2007
    Location
    Dublin, Ireland
    Posts
    72

    Default

    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?
    -----------------------
    John Turner

    http://thoughtforge.net

  4. #4
    Join Date
    Jun 2009
    Posts
    106

    Default 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;
        // ...
    }

Tags for this Thread

Posting Permissions

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