The Session is also used to query the database and retrieve existing persistent objects. Two special methods are provided for the simplest kind of query: retrieval by identifier - The get() and load() methods. The one difference between them is how the indicate that the instance could not be found. If no row with the given identifier value exists in the database, get() return null. The load() method throws an ObjectNotFoundException. It's your choice what error-handling you prefer.
More important, the load() method may return a proxy, a placeholder, without hitting the database. A consequence of this is that you may get an ObjectNotFoundException later, as soon as you try to access the returned placeholder and force its initialization(this is also called lazy loading).
The load() methods always tries to return a proxy, and only returns an initialized object instance if it's already managed by the current persistence context. The get() method on the other hand never returns a proxy, it always hits the database.
More important, the load() method may return a proxy, a placeholder, without hitting the database. A consequence of this is that you may get an ObjectNotFoundException later, as soon as you try to access the returned placeholder and force its initialization(this is also called lazy loading).
The load() methods always tries to return a proxy, and only returns an initialized object instance if it's already managed by the current persistence context. The get() method on the other hand never returns a proxy, it always hits the database.
Comments
Post a Comment