❮ Previous Next ❯

How to develop and execute hibernate application in Eclipse

Dear frnds, now i am going to present how to develop and execute hibernate application in Eclipse to make our objective little easy. For this we have to follow the following points:

  1. Download Eclipse IDE for Java EE Developers

  2. Open Eclipse

  3. Now create a new java project like, File > New > Java Project. Now specify the project name
    e.g. hbdemo then next -finish.

  4. Then we will add jar files. Right click on your project - Build path - Add external archives. Here we use Oracle database for connecting the application, so we need to configure ojdbc14.jar file also.

  5. Create the persistent / POJO class : To create the persistent class, Right click on src - New - Class - specify the class with package name (e.g. java8s) - finish.

Customer.java

package java8s;
public class Customer
{
private int id;
private String name;
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
}
  • Create the mapping file for Persistent class : To create the mapping file, Right click on src - new - file - specify the file name (e.g. customer.hbm.xml) - ok. It must be outside the package.
  • customer.hbm.xml

    Example

    <?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="java8s.Employee" table="customer123">
    <id name="id1">
    <generator class="assigned"> </generator>
    </id>
    <property name="name"></property>
    </class>
    </hibernate-mapping>
  • Create the Configuration file : To create the configuration file, right click on src - new - file. Now specify the configuration file name e.g. hibernate.cfg.xml.
  • hibernate.cfg.xml

    <!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="hbm2ddl.auto">update</property>
    <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
    <property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
    <property name="connection.username">system</property>
    <property name="connection.password">oracle</property>
    <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
    <mapping resource="customer.hbm.xml"/>
    </session-factory>
    </hibernate-configuration>
  • Create the class that retrieves or stores the persistent object : here we store the Customer object to the database.
  • package java8s;
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.Transaction;
    import org.hibernate.cfg.Configuration;
    public class Test
    {
    public static void main(String[] args)
    {
    //creating configuration object
    Configuration cfg=new Configuration();
    cfg.configure("hibernate.cfg.xml");//populates the data of the configuration file
    //creating seession factory object
    SessionFactory sf=cfg.buildSessionFactory();
    //creating session object
    Session s=sf.openSession();
    //creating transaction object
    Transaction t=s.beginTransaction();
    Customer obj=new Customer();
    obj.setId(101);
    obj.setName("Trilochan Tarai");
    s.persist(obj); //persisting the object
    t.commit(); //transaction is committed
    s.close();
    System.out.println("successfully inserted into the database");
    }
    }
  • Then run the application: To run the hibernate application, right click on the class Test - Run As - Java Application.
  • Output

    successfully inserted into the database

    Note: If you will go to the Oracle environment and execute the following sql statement :
    select * from customer123;
    then you will get


    cid cname
    101& Trilochan Tarai&

    ❮ Previous Next ❯

    Follow Us





      Tutorials

    Popular Links

    Contact Us

    address Plot No-741,2ND Floor
    Opp. Bhagabati Temple,Jayadev Vihar
          Bhubaneswar-751013
          Email: info@silantechnology.com
          Phone: 0674-2361252
    address
    39877, sundale dr, apt#101, Fremont, California 94538, USA
           Phone: +1(262)388-7619

    facebook twitter google linked in

    © 2018 Silan Technology. All Rights Reserved