Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

Hibernate in MyEclipse


Here, we are going to create a simple example of hibernate application using myeclipse IDE. For creating the first hibernate application in MyEclipse IDE, we need to follow following steps:

  1. Create the java project
  2. Add hibernate capabilities
  3. Create the Persistent class
  4. Create the mapping file for Persistent class
  5. Add mapping of hbm file in configuration file
  6. Create the class that retrieves or stores the persistent object
  7. Add jar file for oracle
  8. Run the application

1) Create the java project

Create the java project by File MenuNewproject java project . Now specify the project name e.g. firsthb then next finish .


2) Add hibernate capabilities

To add the jar files select your projectclick on MyEclipse menuProject Capabilitiesadd Hibernate capabilitiesnextnext. Now specify the database connection details as displayed in the figure below.

addhbcpb

Here, Check the Enable dynamic table creation check box to create automatic table – next Uncheck the create SessionFactory class because we are going to write the code to get session object self for better understanding – finish.

Now configuration file will be created automatically.


3) Create the Persistent class

Here, we are creating the same persistent class which we have created in the previous topic. To create the persistent class, Right click on src – New – Class – specify the class with package name (e.g. com.Kreationnext.mypackage) – finish.

Employee.java

  1. package com.Kreationnext.mypackage;
  2. public class Employee {
  3. private int id;
  4. private String firstName,lastName;
  5. public int getId() {
  6.     return id;
  7. }
  8. public void setId(int id) {
  9.     this.id = id;
  10. }
  11. public String getFirstName() {
  12.     return firstName;
  13. }
  14. public void setFirstName(String firstName) {
  15.     this.firstName = firstName;
  16. }
  17. public String getLastName() {
  18.     return lastName;
  19. }
  20. public void setLastName(String lastName) {
  21.     this.lastName = lastName;
  22. }
  23. }

4) Create the mapping file for Persistent class

Here, we are creating the same mapping file as created in the previous topic. To create the mapping file, Right click on src – new – file – specify the file name (e.g. employee.hbm.xml) – ok. It must be outside the package. Copy the dtd for this mapping file from this example after downloading it.

employee.hbm.xml

  1. <?xml version=‘1.0’ encoding=‘UTF-8’?>
  2. <!DOCTYPE hibernate-mapping PUBLIC
  3.  “-//Hibernate/Hibernate Mapping DTD 3.0//EN”
  4.  “http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd”>
  5.  <hibernate-mapping>
  6.   <class name=“com.Kreationnext.mypackage.Employee” table=“emp1000”>
  7.     <id name=“id”>
  8.      <generator class=“assigned”></generator>
  9.     </id>
  10.     <property name=“firstName”></property>
  11.     <property name=“lastName”></property>
  12.   </class>
  13.  </hibernate-mapping>

5) Add mapping of hbm file in configuration file

open the hibernate.cgf.xml file, and add an entry of mapping resource like this:

  1. <mapping resource=“employee.hbm.xml”/>

Now the configuration file will look like this:

hibernate.cfg.xml

  1. <?xml version=‘1.0’ encoding=‘UTF-8’?>
  2. <!DOCTYPE hibernate-configuration PUBLIC
  3.           “-//Hibernate/Hibernate Configuration DTD 3.0//EN”
  4.           “http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd”>
  5. <hibernate-configuration>
  6.     <session-factory>
  7.         <property name=“hbm2ddl.auto”>update</property>
  8.         <property name=“dialect”>org.hibernate.dialect.Oracle9Dialect</property>
  9.         <property name=“connection.url”>jdbc:oracle:thin:@localhost:1521:xe</property>
  10.         <property name=“connection.username”>system</property>
  11.         <property name=“connection.password”>oracle</property>
  12.         <property name=“connection.driver_class”>oracle.jdbc.driver.OracleDriver</property>
  13.     <mapping resource=“employee.hbm.xml”/>
  14.     </session-factory>
  15. </hibernate-configuration>

6) Create the class that retrieves or stores the persistent object

In this class, we are simply storing the employee object to the database.

  1. package com.Kreationnext.mypackage;
  2. import org.hibernate.Session;
  3. import org.hibernate.SessionFactory;
  4. import org.hibernate.Transaction;
  5. import org.hibernate.cfg.Configuration;
  6. public class StoreData {
  7. public static void main(String[] args) {
  8.     //creating configuration object
  9.     Configuration cfg=new Configuration();
  10.     cfg.configure(“hibernate.cfg.xml”);//populates the data of the configuration file
  11.     //creating seession factory object
  12.     SessionFactory factory=cfg.buildSessionFactory();
  13.     //creating session object
  14.     Session session=factory.openSession();
  15.     //creating transaction object
  16.     Transaction t=session.beginTransaction();
  17.     Employee e1=new Employee();
  18.     e1.setId(115);
  19.     e1.setFirstName(“sonoo”);
  20.     e1.setLastName(“jaiswal”);
  21.     session.persist(e1);//persisting the object
  22.     t.commit();//transaction is commited
  23.     session.close();
  24.     System.out.println(“successfully saved”);
  25. }
  26. }

7) Add the jar file for oracle (ojdbc14.jar)

To add the ojdbc14.jar file, right click on your project – build path – add external archives – select the ojdbc14.jar file – open.

8) Run the application

Before running the application, determine that directory structure is like this.

dirmy

To run the hibernate application, right click on the StoreData class – Run As – Java Application.

download the hibernate example

 

 

Comments

comments