Hibernate learnings

Few of the important key points to remember in Hibernate:

1.) Use Transactional boundaries in your service layer.

2.) Outside a transaction boundary if session gets closed, the proxy object gets detached and hence if there is a collection which is lazily loaded, it can not be fetched. Either make separate call for the collection or use OpenSessionViewFilter.

3.)Use session.load() when you want to return proxy and need to property of object, just need reference with Id to be added/removed. No DB call is made.

4.)Use session.get() when you want to make a DB call and access actual object. You do it when you want to access/modify any property of the returned object.

5.) If you modify an object in persistent state it will automatically sync with DB when you commit the transaction (flush the session), it is called automatic Dirty checking.

6.) There are 4 states in hibernate :

a.)  Transient -> When object is created.

b.) Persistent-> When save/update/persist is called ,

c.)Detached-> after session is closed.

d.) Removed-> after calling delete/remove.

7.) We can use either programmatic or declarative Transactions, declarative are preferred as it gives pluggability.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s