Hibernate
Hibernate
What is Hibernate
Hibernate is a free, open source Java package that makes it easy to work with relational databases. Hibernate makes it seem as if your database contains plain Java objects like you use every day, without having to worry about how to get them out of (or back into) mysterious database tables.
Hibernate liberates you to focus on the objects and features of your application, without having to worry about how to store them or find them later.
Why Hibernate?
Cost effective. Learning is very easy compare to EJB.
Hibernate Architecture
Hibernate Architecture
Hibernate architecture has three main components: Connection Management Hibernate Connection management service provide efficient management of the database connections. Database connection is the most expensive part of interacting with the database as it requires a lot of resources of open and close the database connection.
Transaction management: Transaction management service provide the ability to the user to execute more than one database statements at a time.
Object relational mapping: Object relational mapping is technique of mapping the data representation from an object model to a relational data model. This part of the hibernate is used to select, insert, update and delete the records form the underlying table. When we pass an object to a Session.save() method, Hibernate reads the state of the variables of that object and executes the necessary query.
Hibernate is very good tool as far as object relational mapping is concern but in terms of
connection management and transaction management, it is lacking in performance and capabilities. So usually hibernate is being used with other connection management and transaction management tools. For example apache DBCP is used for connection pooling with the Hibernate.
Hibernate 3.0
Hibernate 3.0 provides three full-featured query facilities: Hibernate Query Language Hibernate Criteria Query API Hibernate Native Query
1. Configuring Hibernate
Hibernate uses the hibernate.cfg.xml to create the connection pool and setup required environment. <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver </property> <property name="hibernate.connection.url">jdbc:mysql://localhost/hibernate </property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password"></property> <property name="hibernate.connection.pool_size">10</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.hbm2ddl.auto">update</property> <!-- Mapping files --> <mapping resource="contact.hbm.xml"/> </session-factory> </hibernate-configuration>
2. Persistence Class
Hibernate uses the Plain Old Java Objects (POJO) classes to map to the database table. We can configure the variables to map to the database column.
package Example;
public class Contact { private String firstName; private String lastName; private String email; private long id; public String getEmail() { return email; } public String getFirstName() { return firstName; } public String getLastName() { return lastName; } public void setEmail(String string) { email = string; } public void setFirstName(String string) { firstName = string; } public void setLastName(String string) { lastName = string; } public long getId() { return id; } public void setId(long l) { id = l; } }
<property name="firstName"> <column name="FIRSTNAME" /> </property> <property name="lastName"> <column name="LASTNAME"/> </property> <property name="email"> <column name="EMAIL"/> </property> </class> </hibernate-mapping>
Thank you