Saturday, May 22, 2010

My First Hibernate Program.....

This is my first Hibernate program...... using ECLIPSE IDE

I will be using Mysql as my database engine......

Database Name : student 
Table : user


user will have two fields id(varchar and it is primary key) and name(varchar).

Now lets start
Steps:
1) Create a Dynamic Web Project.Name it firstHibernate
2) Copy required jar files into lib directory of the project folder.

lib directory : WebContent/WEB-INF/lib


3) after that create directory structure like this : 

creat resource folder which will hold hibernate configuration(hibernate.cfg.xml) and mapping file(student.hbm.xml).



4)Copy and paste this code in hibernate.cfg.xml file: 
-------------------------------------------------------------------------------------------------------------
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/student
</property>
<property name="connection.username">root</property>
<property name="connection.password">mitesh</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache  -->
<property name="cache.provider_class">
org.hibernate.cache.NoCacheProvider
</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>
<mapping resource="student.hbm.xml"/>
</session-factory>
</hibernate-configuration>
-------------------------------------------------------------------------------------------------------------


Note : 
three thing you need to change according to the system :
1)Database name : if you have created database other than student then change it with 
jdbc:mysql://localhost:3306/YourDatabaseName
2) root that is default username and mitesh that is password for username root. I haven't changed if you have changed it then change it with 
userName
password


5) Now Create a mapping file student.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="mitesh.first.Student" table="user">
   <id name="id" type="long" column="id" >
   <generator class="assigned"/>
  </id>
  <property name="name">
     <column name="name" />
  </property>
 </class>
</hibernate-mapping>
-------------------------------------------------------------------------------------------------------------

here table specifies table that we have created in database student that is user. and mitesh.first.Student specifies the fully qualified location of the bean class Student.

column name specifies name of the column in user table. 


6) Now creat a package named mitesh.first under src folder and after that create a class named Student.java.
copy and paster this code into Student.java class

-------------------------------------------------------------------------------------------------------------
package mitesh.first;
public class Student {
  private String name;
  private long id;
  public String getName() {
    return name;
  }
  public void setName(String string) {
    name = string;
  }
  public long getId() {
    return id;
  }
  public void setId(long l) {
    id = l;
  }
}
-------------------------------------------------------------------------------------------------------------

6)
after that create a java file in mitesh.first package containing main method lets give name FirstHibernate.java
copy and paste the following code :

-------------------------------------------------------------------------------------------------------------
package mitesh.first;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class FirstHibernate {
  public static void main(String[] args) {
    Session session = null;
    try{      
      SessionFactory sessionFactory = new 
Configuration().configure().buildSessionFactory();
       session =sessionFactory.openSession();
         System.out.println("Inserting Record");
        Student student = new Student();
        student.setId(31);
        student.setName("Mitesh");
        session.save(student);
        System.out.println("Done");
    }catch(Exception e){
      System.out.println(e.getMessage());
    }finally{
      // Actual contact insertion will happen at this step
      session.flush();
      session.close();
      }
  }
}

we are all set.................





No comments:

Post a Comment