Hibernate makes use of persistent objects commonly called as POJO (POJO = "Plain Old Java Object".) along with XML mapping documents for persisting objects to the database layer. The term POJO refers to a normal Java objects that does not serve any other special role or implement any special interfaces of any of the Java frameworks (EJB, JDBC, DAO, JDO, etc...).
Rather than utilize byte code processing or code generation, Hibernate uses runtime reflection to determine the persistent properties of a class. The objects to be persisted are defined in a mapping document, which serves to describe the persistent fields and associations, as well as any subclasses or proxies of the persistent object. The mapping documents are compiled at application startup time and provide the framework with necessary information for a class. Additionally, they are used in support operations, such as generating the database schema or creating stub Java source files.
Rather than utilize byte code processing or code generation, Hibernate uses runtime reflection to determine the persistent properties of a class. The objects to be persisted are defined in a mapping document, which serves to describe the persistent fields and associations, as well as any subclasses or proxies of the persistent object. The mapping documents are compiled at application startup time and provide the framework with necessary information for a class. Additionally, they are used in support operations, such as generating the database schema or creating stub Java source files.
Why Hibernate :
- Hibernate was introduced to address the issues of Entity Beans
- Hibernate is built on top of JNDI, JDBC, JTA
- It uses XML based configuration files for mapping
- Supports many databases like Sybase, Oracle, MySQL, other Object Oriented Databases etc.
- Easy migration from one vendor database to another
- Hibernate generates the JDBC Code based on the underlying vendor database
- Hibernate APIs are very simple to learn and use
- Provides quite powerful object query language known as Hibernate Query Language (HQL)
- Hibernate takes care of mapping Java classes to database tables using XML files and without writing any line of code.
- Provides simple APIs for storing and retrieving Java objects directly to and from the database.
- If there is change in Database or in any table then the only need to change XML file properties.
- Abstract away the unfamiliar SQL types and provide us to work around familiar Java Objects.
- Hibernate does not require an application server to operate.
- Manipulates Complex associations of objects of your database.
- Minimize database access with smart fetching strategies.
- Provides Simple querying of data.
Public class Trade
{
Private Long tradeId;
Private String clientId;
Private String symbol;
Private String orderType;
Public Long getTradeId() { return id; }
Private void setTradeId(Long id) { this.tradeId = id; }
Public String getSymbol() { return symbol; }
Public void setSymbol(String text) { this.symbol = text;}
}
Hibernate Persistence Code
…
//Initialize Hibernate session
Session session = getSessionFactory().openSession();
//Start the transaction
Transaction tx = session.beginTransaction();
//Object to be persisted
Trade trade = new Trade();
//Set the object values
//Persist the Object
session.save(trade);
//Commit the transaction
tx.commit();
//Close the Hibernate Session
session.close();
….
//Initialize Hibernate session
Session session = getSessionFactory().openSession();
//Start the transaction
Transaction tx = session.beginTransaction();
//Object to be persisted
Trade trade = new Trade();
//Set the object values
//Persist the Object
session.save(trade);
//Commit the transaction
tx.commit();
//Close the Hibernate Session
session.close();
….
How is Hibernate Persisting?
Hibernate used XML Mapping file to generate the SQL code to save the object
<hibernate-mapping>
<class name=“Trade" table=“Trades">
<id name=“tradeId" column=“TRADE_ID"></id>
<property name=“clientId" column=“CLIENT_ID"/>
<property name=“symbol" column=“SYMBOL"/>
<property name=“orderType" column=“ORDER_TYPE"/>
</class>
</hibernate-mapping>
Hibernate Generates SQL Statement like
INSERT INTO Trades (TRADE_ID, CLIENT_ID, SYMBOL, OR
DER_TYPE)
VALUES (30, “CL7678”, “IBM”, “M”)
INSERT INTO Trades (TRADE_ID, CLIENT_ID, SYMBOL, OR
DER_TYPE)
VALUES (30, “CL7678”, “IBM”, “M”)
RDBMS Table
Create Table Trades
(
TRADE_ID int not null,
CLIENT_ID varchar(50),
SYMBOL varchar(15),
ORDER_TYPE char(1)
)
Create Table Trades
(
TRADE_ID int not null,
CLIENT_ID varchar(50),
SYMBOL varchar(15),
ORDER_TYPE char(1)
)