RamValli
RamValli

Reputation: 4475

difference between getReference() and load()

In Hibernate 4.3 , load() from session returns the proxy object with lazy loading and get() returns the object if it exists or null if it doesn't. But here and here its mentioned that getReference() returns a proxy object and load() returns an object with initialized data. On Further googling i realized that these methods are from IdentifierLoadAccess Interface. so my questions are

  1. Is this getReference() is functionally equal to load() from session and this load() is equal to get() from session?
  2. Is this Interface is part of JPA specification? or from Hibernate Native API?
  3. What is the need for these methods when they have these functionalities are already covered in session interface?

Upvotes: 4

Views: 4525

Answers (2)

Ken Chan
Ken Chan

Reputation: 90527

  1. EntityManager#getReference() is functionally equal to session#load(). This can be verified by the hibernate 's EntityManager implementation ( AbstractEntityManagerImpl) which delegates the work to session#load().

    session#load() and session#get() have some difference in behaviour . For details , please see this.

  2. IdentifierLoadAccess is under the package org.hibernate . So it is Hibernate native API . All interfaces defined by JPA specification are under the package javax.persistence.

  3. JPA are standard Java API for persistence which means if your application only uses JPA API , theoretically , it is portable between different JPA providers. You application would work even you change to use other JPA provider only switch to other JavaEE application server.

Upvotes: 3

JB Nizet
JB Nizet

Reputation: 692071

The package of IdentifierLoadAccess is org.hibernate. It's thus not part of the JPA spec. Otherwise, it would be under javax.persistence.

IdentifierLoadAccess.getReference() is, as the example suggests, equivalent to EntityManager.getReference(), and also to the old, badly named Session.load().

IdentifierLoadAccess.load() is, as the example suggests, equivalent to EntityManager.find(), and also to the Session.get().

As far as I understand, this IdentifierLoadAccess interface and the methods it contains are useful to provide a consistent way to load/getReference to entities between IDs and simple natural IDs (and to a lesser extent, natural IDs). See all the methods starting by by in the Session api doc.

Upvotes: 2

Related Questions