Sunday, November 10, 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.

  1. BOOK_ID (Primary Key, Type: Number) driven by sequence book_id_seq
  2. BOOK_NAME (Type: Varchar2(50)
  3. PUBLISH_DATE (Type: Date)
  4. 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=GenerationType.SEQUENCE, generator=BOOK_SEQUENCE)
    private long number;
    
    @Column(name="BOOK_NAME")
    private String name;
    
    @Temporal(DATE)
    @Column(name="PUBLISH_DATE")
    private Date publishDate; 
    
    @Transient
    private boolean isPopular;
    
    @Column(name="BOOK_PREVIEW")
    @Lob
    @Basic(fetch=FetchType.LAZY)
    private String preview;
    
    public Book(){
    }
    
    //setters and getters

}

The annotations used above are described in detail:
  1. @table is used to map the class to a particular table. There are other options here like the inheritance strategy, which will be explained later.
  2. @column is used to map a field to a particular column. This annotation could be applied at field level or at method level (getter). It is advised to follow a common practice throughout.
  3. @Id is used to indicate primary key columns with an appropriate ID generator strategy as explained in the previous post. In the above example, we are using sequence generator strategy. The sequence name is mapped through 'sequenceName' option.
  4. @temporal is used to map date or timestamp fields. Possible values being TIME/DATE/TIMESTAMP.
  5. @Transient is used for fields which are not persistent and which will not be saved/retrieved to/from the database. In the above example, isPopular could be a derived field and  hence marked as transient.
  6. @lob is used to map CLOB or BLOB columns. In the above example, preview provides a brief description of the book and hence is a CLOB column. For blob types, you might want to use byte[] data type in java to represent the blob data.
  7. FetchType.Lazy is used to indicate lazy loading. By default, hibernate performs eager loading, which means all the properties are loaded when the entity is loaded. This strategy would cause performance issues when the property is a clob/blob field. In the above example, 'preview' is loaded lazily. i.e, loaded when the first call to the getPreview() is performed. 

Dynamic Inserts/Updates:

In the above example, we have annotated the Book class with Entity annotation from the hibernate package. The first one is from 'javax.persistence.Entity' package. The hibernate Entity annotation has two options 'dynamicInsert' and 'dynamicUpdate'.

What this means is that, while inserting or updating the Book table, Hibernate will insert only those columns whose values are non null. SImilarly, if dynamicUpdate is true, Hibernate will update only those columns whose values are not modified. Hibernate will keep track of which columns are modified in the current session. 
These two options will cause an increase in performance, if your tables have large number of columns and you have scenarios wherein, not all columns are inserted/updated every time. 

However, use this option with caution. If you blindly apply this strategy in every single case, then it might actually backfire. Since Hibernate has to keep dynamically prepare the statements and also has to apply additional conditions for null checking or checking for modified fields, this might actually bring down performance.  So exercise this option with care.

                                       Continued.....

No comments:

Post a Comment