Thursday, April 30, 2009

Hibernate Object States and Actions



Transient
an object is transient if it has just been instantiated using the new operator, and it is not associated with a Hibernate Session. It has no persistent representation in the database and no identifier value has been assigned. Transient instances will be destroyed by the garbage collector if the application doesn't hold a reference anymore. Use the Hibernate Session to make an object persistent (and let Hibernate take care of the SQL statements that need to be executed for this transition).

Persistent
a persistent instance has a representation in the database and an identifier value. It might just have been saved or loaded, however, it is by definition in the scope of a Session. Hibernate will detect any changes made to an object in persistent state and synchronize the state with the database when the unit of work completes. Developers don't execute manual UPDATE statements, or DELETE statements when an object should be made transient.

Detached
a detached instance is an object that has been persistent, but its Session has been closed. The reference to the object is still valid, of course, and the detached instance might even be modified in this state. A detached instance can be reattached to a new Session at a later point in time, making it (and all the modifications) persistent again. This feature enables a programming model for long running units of work that require user think-time. We call them application transactions, i.e. a unit of work from the point of view of the user.

save
stores an object into the database. That means it insert an entry if the identifier doesn't exist, else it will throw error. If the primary key already present in the table, it cannot be inserted.

update
is used for updating the object using identifier. If the identifier is missing or doesn't exist, it will throw exception.

saveOrUpdate
calls save() or update() based on the operation. If the identifier exists, it will call update method else the save method will be called.

persist
does the same like session.save(). But session.save() return Serializable object but session.persist() return void.

lock
simply reattaches the object to the session without checking or updating the database on the assumption that the database in sync with the detached object. It is the best practice to use either session.update(..) or session.saveOrUpdate(). Use session.lock() only if you are absolutely sure that the detached object is in sync with your detached object or if it does not matter because you will be overwriting all the columns that would have changed later on within the same transaction.When you reattach detached objects you need to make sure that the dependent objects are reatched as well.

get and load
Both methods create a persistent object by loading the required object from the database. But if there was not such object in the database then the method load() throws an exception whereas get() returns null.

1 comment:

  1. Hi Henry can u briefly explain with examples of
    these methods.

    ReplyDelete