Home > Uncategorized > Hibernate Session's get() annd load() comparison

Hibernate Session's get() annd load() comparison

These are the differences I could readily understand when I was comparing the runtime behaviour of Hibernate Session’s get and load methods.

a)    get() does a database hit as soon as the method is called, load() makes a database hit, only if one of the field in the entity is called.

b)    load() should be used when the entity is assured that it exists in the database, else get() should be called. If an non-existent primary key is passed, get() returns a null directly. However passing a non-existing primary key to load() returns you a non-null entity object, but would throw this exception when a field of that entity is referenced: org.hibernate.ObjectNotFoundException: No row with the given identifier exists.

c)    calling a field in the entity in load() method after getTransaction().commit(), would throw a LazyInitializationException (could not initialize proxy – no Session).


Categories: Uncategorized Tags:
  1. No comments yet.
  1. No trackbacks yet.