14

EclipseLink can load lazy relationships in entities even after the entity manager that has created them is no longer available. With Hibernate this doesn't work, or at least didn't at the time of that post.

What about other providers? OpenJPA and DataNucleus in particular?

What are the downsides to this (except more complex implementation)?

Alexey Romanov
  • 154,018
  • 31
  • 276
  • 433
  • 1
    That article mentions lazy loading when you close the database connection. There is no mention of closing the EntityManager. An EntityManager can represent multiple connections over time. – DataNucleus Apr 16 '12 at 07:00
  • @DataNucleus See http://stackoverflow.com/a/8491416/9204 and https://forums.oracle.com/forums/thread.jspa?messageID=1706796 – Alexey Romanov Apr 16 '12 at 10:02
  • sure but that is not what the post you referred to is about, nor the title of your post here "... outside transactions" – DataNucleus Apr 16 '12 at 11:20

3 Answers3

13

Although Hibernate does require the same EntityManager to be available in order to lazily load objects, it is easy to achieve flexible lazy loading with the Open Session in View Pattern. In essence, you keep the EntityManager open as long as you need it. I've developed client-side applications that keep the same EntityManager open as long as the application is open. This would give you essentially the same behavior described in the article. However, it is certainly more difficult to implement than the "out of the box" lazy loading Roman describes.

All that being said, there are downsides to lazy loading. The developer has to be aware of his fetching strategy and must be capable of differentiating when and where each strategy is most applicable. Otherwise you can end up with serious performance issues like the N+1 Select Problem. Plus there's always the possibility that you'll get a database exception during the rendering of the view.

About OpenJPA and DataNucleus: Although I've never used either, this post indicates that OpenJPA also requires an OpenSessionInViewFilter for lazy loading. This SO answer and this forum post indicate that DataNucleus requires an OpenPersistenceManagerInViewFilter for lazy loading.

Community
  • 1
  • 1
Tim Pote
  • 24,528
  • 6
  • 58
  • 63
8

Note that Hibernate 4.1.6 added support for loading lazy data outside transactions via the hibernate.enable_lazy_load_no_trans JPA property.

Doesn't seem to be very widely used / known about - the only official documentation seems to be a feature ticket - so probably worth applying some caution.

In my (limited) experience, it generally seems to work well except that it doesn't seem to fetch entities on the "mapped" side of bi-directional relationships.

Steve Chambers
  • 31,993
  • 15
  • 129
  • 173
5

If you have no EntityManager you have no knowledge of the datastore, the EMF or anything. So no you can't do lazy loading then (other than embodying that info in your object yourself) if you want to be portable ... i.e that is outside the JPA spec.

DataNucleus JPA will work perfectly well performing lazy loading of fields outside of transactions. Obviously you need to state if you are running with TRANSACTION or EXTENDED persistence context, because in the former case the objects get DETACHED at commit of the transaction (and you can't lazy-load once detached), and in the latter the objects remain MANAGED (and you can lazy-load in that situation).

DataNucleus
  • 15,199
  • 3
  • 30
  • 37