Showing posts from November, 2013

Hibernate Tutorial - Part II

For part I refer:
Hibernate tutorial Part - I

In this post we will see how the entity mapping is done. Lets take an example of a library management application for simplicity and take a look at the various possible mappings.

Lets say we need to create an entity 'Book'. We will create a Book POJO. This will be mapped to a table named 'Book'. The book table will have the following columns.

BOOK_ID (Primary Key, Type: Number) driven by sequence book_id_seqBOOK_NAME (Type: Varchar2(50)PUBLISH_DATE (Type: Date)BOOK_PREVIEW (Type: clob)The Book class with entity annotations will be as below:

@Entity @Table(name="book") @org.hibernate.annotations.Entity( dynamicUpdate = true ) public class Book implements Serializable{ private static final String BOOK_SEQUENCE = "book_id_seq"; @Id @Column(name="BOOK_ID") @SequenceGenerator( name=BOOK_SEQUENCE, sequenceName=BOOK_SEQUENCE) @GeneratedValue(strategy=Ge…

Hibernate Tutorial - Part I

Overview:Hibernate is an Object relational mapping (ORM) tool which will help us map Java objects to the underlying database objects. It will help us perform various operations on objects and hibernate will translate it to the DBMS operations and saves us from writing sql queries.
An equally important aspect is the importance of object orientated principles which need not be compromised when we use Hibernate. If we go with plain old sql style of design, then due to the disconnect between the object oriented design and the DBMS design, often the object oriented design is compromised.
For instance, concepts like inheritance, composition do not have a corresponding representation in the DBMS world. Similarly, we cannot truly represent a bi-drectional navigation and mapping between two entities (say for eg, Author and Book) in the DBMS world. These short comings could be overcome if we use Hibernate and we can minimize the influence of DBMS design on the object oriented design.
When we use H…