Hibernate Application Java

  • Hibernate framework simplifies the development of java application to interact with the database. Hibernate is an open source, lightweight  tool. 

  • An ORM tool simplifies the data creation, data manipulation and data access. It is a programming technique that maps the object to the data stored in the database.

Example

Student.java

package com.hibernate;
/**
 *
 * @author ananddw
 *
 */
public class Student {
   
    private int id;
    private String studentName;
    private String studentPlace;
   
    //Setter and Getters
   
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getStudentName() {
        return studentName;
    }
    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }
    public String getStudentPlace() {
        return studentPlace;
    }
    public void setStudentPlace(String studentPlace) {
        this.studentPlace = studentPlace;
    }
   
   

}

 ==========StudentResourc.java ====================
package com.hibernate;
import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
import org.hibernate.Transaction; 
import org.hibernate.cfg.Configuration; 

/**
 *
 * @author ananddw
 *
 */
public class StudentResource { 
public static void main(String[] args) { 
    StudentResource studentresrouce=new StudentResource();
    studentresrouce.saveObject();
}
public boolean saveObject(){
    boolean flag=false;
try{  
  Configuration cfg=new Configuration(); 
  cfg.configure("hibernate.cfg.xml");
  SessionFactory factory=cfg.buildSessionFactory();  
  Session session=factory.openSession(); 
  Transaction t=session.beginTransaction();
 
  Student student = new Student();
  student.setId(3);
  student.setStudentName("Rahul");
  student.setStudentPlace("Mumbai");
  session.saveOrUpdate(student);
   
  t.commit();
  session.close(); 
   
  System.out.println("successfully saved"); 
}catch(Exception e){
    e.printStackTrace();
    flag=false;

  return flag;

}

=============hibernate.cfg.xml ========================





<?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>
        <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
        <property name="hibernate.connection.url">jdbc:postgresql://127.0.0.1:5432/hibernate</property>
        <property name="hibernate.connection.username">postgres</property>
        <property name="hibernate.connection.password">password</property>
        <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>
        <property name="hibernate.hbm2ddl.auto">update</property>

        <mapping resource="com/hibernate/student.cfg.xml" />


    </session-factory>
</hibernate-configuration>




==================student.cfg.xml=============

<?xml version='1.0' encoding='UTF-8'?> 
    <!DOCTYPE hibernate-mapping PUBLIC 
     "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
     
     <hibernate-mapping> 
      <class name="com.hibernate.Student" table="student"> 
        <id name="id"> 
         <generator class="native"></generator> 
        </id> 
               
        <property name="studentName"></property> 
        <property name="studentPlace"></property> 
               
      </class> 
               
     </hibernate-mapping>



PS: Please Add Required Jars to run hibernate Application


hibernate3.jar
dom4j-1.6.1.jar
commons-logging-1.1.1.jar
commons-collections-2.1.1.jar
postgresql-8.3-603.jdbc3.jar
cglib-2.1_3.jar
cglib-nodep-2.1_3.jar
asm-1.5.2.jar
asm-analysis-1.5.2.jar
asm-attrs-1.5.2.jar
asm-tree-1.5.2.jar
asm-util-1.5.2.jar
asm-xml-1.5.2.jar
jta.jar
antlr.jar





Comments

Popular posts from this blog

Converting JSONArray into CSV file in java

Online Book Store Website Using Java

How to read CSV File in Java