DISQUS

Dobesland: Hibernate + fetch=FetchType.LAZY making trouble | Dobesland

  • Dmitri Livotov · 2 years ago
    Are you sure you did not close or clear the session or transaction before try to navigate your lazy object ? This is usually the typical trouble with lazy objects I also encountered with when first using HIbernate. In case of pure EJB (JPA), you can get the same behavior when returning lazy object as a result of EJB bean method.
  • dobes · 2 years ago
    Yeah, I'm in a transaction - it's one EJB using another one. I'm using reflection to fetch properties of an entity and comparing them using equals() and, for whatever reason, equals() returned false when I compared an object to itself! Weird!

    During debugging, I saw that the object was a lazy proxy, and that it was not loaded, so I changed FetchType.LAZY to FetchType.EAGER and suddenly it worked!


    I made sure my equals() method wasn't accessing members directly - it was using getter methods. So, I have no idea why this was not working this way.

    Some kind of issue with accessing members with reflection? Or something else?

    I guess I just don't feel like I have time to debug these weird hibernate behaviors when I can just not use lazy fields.

    After making this post, though, I did switch some of my inverse relationships back to LAZY because they were causing all kinds of extra things to be fetched that I didn't need... hopefully I won't get burned by that too soon.