Topic : Hibernate
By : Noman Islam
Outline
What is a framework?
Persistence Framework
What is hibernate ?
Four Steps
Defining the Persistent class (User.java)
public class User {
private Long id;
private String userName;
private String userPassword;
private String userEmail;
public User() { }
public Long getId() { return this.id; }
public void setId(Long id) { this.id = id; }
public String getUserName() { return this.userName; }
public void setUserName(String userName) { this.userName = userName; }
public String getUserPassword() { return this.userPassword; }
public void setUserPassword(String userPassword) { this.userPassword = userPassword; }
public String getUserEmail() { return this.userEmail; }
public void setUserEmail(String userEmail) { this.userEmail = userEmail; }
}
XML mapping document (User.hbm.xml)
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="User" table="user">
<id column="USER_ID" name="id" type="java.lang.Long">
<generator class="increment"/>
</id>
<property column="USER_NAME"
length="20"
name="userName" not-null="true"
type="java.lang.String"/>
<property column="USER_PASSWORD"
length="10"
name="userPassword" not-null="true"
type="java.lang.String"/>
<property column="USER_EMAIL"
length="20"
name="userEmail" type="java.lang.String"/>
</class>
</hibernate-mapping>
Configuration file (hibernate.cfg.xml)
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/ hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="hibernate.dialect">
org.hibernate.dialect.MySQLDialect </property>
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver </property>
<property name="hibernate.connection.url">
jdbc:mysql://localhost:3306/quickstart
</property>
<property name="hibernate.connection.username">
root
</property>
<property name="hibernate.connection.password">
</property>
<!-- Mapping files -->
<mapping resource="User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Inserting new records (UserInsertHibernate.java)
import org.hibernate.*;
import org.hibernate.cfg.*;
public class UserInsertHibernate {
public static void main(String[] args)
throws Exception {
// Fire up Hibernate
SessionFactory sessionFactory = new Configuration()
.configure().buildSessionFactory();
// Open Session
Session session = sessionFactory.openSession();
// Build a User
User user = new User();
Transaction tx = session.beginTransaction();
user.setUserEmail("user@yahoo.com");
user.setUserName("userName-1");
user.setUserPassword("Password-1");
// Save User and close Session
session.saveOrUpdate(user);
session.close();
}
}
Hibernate Query Language (HQL)
"select product from product "
+ "in class test.hibernate.Product "
+ "where product.name=:name";
// search for what?
String name = "noman";
// search and return
List list = session.find(query, name,
Hibernate.STRING);
Conclusion
References
[1] Applying UML and Pattern by Craig Larmen
Questions (if any)