Let's
Start Developing First Hibernate Application.
To
create Hibernate standalone application we need following things :
1.
Hibernate Library Files :
The following .jar(s) will required -
- antlr-2.7.6.jar
- commons-collections-3.1.jar
- commons-logging-1.1.1.jar
- commons-logging-api-1.1.1.jar
- dom4j-1.6.1.jar
- Hibernate3.har
- hibernate-jpa-2.0-api-1.0.1.Final.jar
- javassist-3.12.0.GA.jar
- jta-1.1.jar
- log4j-1.2.16.jar
- mysql-connector-java-5.1.15-bin.jar
- slf4j-api-1.6.1.jar
- slf4j-log4j12-1.6.1.jar
2.
Hibernate Configuration File i.e. hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
<hibernate-configuration>
<session-factory>
<property
name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property
name="hibernate.connection.password">root</property>
<property
name="hibernate.connection.url">jdbc:mysql://localhost/employee</property>
<property
name="hibernate.connection.username">root</property>
<property
name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property
name="hibernate.hbm2ddl.auto">create</property>
<mapping
resource="com/hibernate/app/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
3. A
POJO class say User
package com.hibernate.employee;
public class User {
private long userId;
private String username;
public long getUserId() {
return userId;
}
public void setUserId(long userId) {
this.userId = userId;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
4.
Hibernate Mapping File say User.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate
Mapping DTD 3.0//EN"
<!-- Generated Jun 16, 2013 10:57:01 AM by Hibernate Tools
3.3.0.GA -->
<hibernate-mapping
package="com.hibernate.employee">
<class name="User"
table="USER">
<id
name="userId" type="long">
<column name="USERID" />
<generator class="increment" />
</id>
<property
name="username" type="java.lang.String" >
<column name="USERNAME" />
</property>
</class>
</hibernate-mapping>
5.
Client class
package com.hibernate.client;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.hibernate.employee.User;
public class UserClient {
public static void main(String[] args) {
//build session factory
SessionFactory sessionFactory = new
Configuration().configure().buildSessionFactory(); //sessionfactory is
expensive
//open session
Session session = sessionFactory.openSession();
//begin transction
Transaction transaction = session.beginTransaction();
User user 1= new User();
user1.setUsername("User Name 1");
session.save(user1);
User user2 = new User();
user2.setUsername("User Name 2");
session.save(user2);
//commit transaction
transaction.commit();
//close session
session.close();
}
}
This
application will store an object in the database & create a mapping file
for the same.