Saturday, November 3, 2012

How to write hibernate code in java application


To use hibernate first setup the environment of hibernate .After that need to create some files in application as-

1. Hibernate.cfg.xml file contain database configuration,connection pool,user,username,password and other sql information. the main thing is that we can not change file name the name always as it is.

<hibernate-configuration>
<session-factory>
  <property name="hibernate.connection.driver_class">
     com.mysql.jdbc.Driver</property>
  <property name="hibernate.connection.url">
jdbc:mysql://localhost:3306/hibern</property>
  <property name="hibernate.connection.username">root</property>
  <property name="hibernate.connection.password">root</property>
  <property name="hibernate.connection.pool_size">10</property>
  <property name="show_sql">true</property>
  <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
  <property name="hibernate.hbm2ddl.auto">update</property>
  <!-- Mapping files -->
  <mapping resource="createtable.hbm.xml">
 </mapping></session-factory>
  </hibernate-configuration>



Here property   <property name="hibernate.connection.driver_class">   is use to load the driver of particular                     database and    <mapping resource="createtable.hbm.xml"/>    property define the mapping file which contain the mapping with database table.

2.Mapping file (Create.hbm.xml) file contain the mapping of the table as-

            <hibernate-mapping>
               <class name="com.test.CreateTable" table="CONTACT">
               <id column="ID" name="id" type="long">
               <generator class="increment">
               </generator></id>

                <property name="firstName">
                <column name="FIRSTNAME">
                </column></property>
               <property name="lastName">
               <column name="LASTNAME">
               </column></property>
               <property name="email">
               <column name="EMAIL">
               </column></property>
               </class>
              </hibernate-mapping>

Here class name define the java file which contain the bean file who have id,firstname,lastname,email variables with getter and setter properties and these properties is mapped with database table here no need to create database table when code is execute automatically create a table with that fields.

3. bean file is use to inject the object values-


package com.test;

import java.io.Serializable;

public class CreateTable  implements Serializable{
private String firstName;
 private String lastName;
 private String email;
 private long id;
 public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
}


4. Main Class to do the transaction with the database -

package com.test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class Main {
public static void main(String[] args) throws Exception{
 Session session = null;
SessionFactory  sessionfactory;
sessionfactory = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();
session =sessionfactory.openSession();
 //Create new instance of CreateTable and set
session.beginTransaction();
System.out.println("Inserting Record");
 CreateTable createTable = new CreateTable();
 createTable.setFirstName("navin");
 createTable.setLastName("bansal");
 createTable.setEmail("nav@gmail.com");
 session.save(createTable);
                   session.getTransaction().commit();
 System.out.println("Done");
                  session.flush();
 session.close();
 }
}

now execute this file and a record is automatically inserted is your database table.
Output-
Inserting Record
Hibernate: select max(ID) from CONTACT
Hibernate: insert into CONTACT (FIRSTNAME, LASTNAME, EMAIL, ID) values (?, ?, ?, ?)
Done

yupee!!!!!!!!!  the code is executed.now see the table-


For Further Reading,
General, Hibernate, How To, Java

0 comments:

Post a Comment


 

Site Status

Man Behind Technical Today

Hello, I am Navin Bansal. I am a student of MCA in Rajsthan Institute of Engineering and Technology and owner of this blog. I share my view and ideas among people.