0% found this document useful (0 votes)
66 views17 pages

Hibernate in Eclipse

Hibernate is a DataBase technology used to store and retrieve data from a DataBase. It is a part of the java(core) and java(advanced) frameworks. Hibernates can be used to store, retrieve and update data.

Uploaded by

Kirti Waykole
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views17 pages

Hibernate in Eclipse

Hibernate is a DataBase technology used to store and retrieve data from a DataBase. It is a part of the java(core) and java(advanced) frameworks. Hibernates can be used to store, retrieve and update data.

Uploaded by

Kirti Waykole
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 17

Home About Us Contact Us Services Site Map

Home Java(Core) Java(Advance) Framework DataBase Tools Servers IDEs PHP Interview Questions Home Framework Hibernate

Tutorials
Struts 2 Struts 1 jQuery Hibernate Hibernate JPA Spring Java Server Faces (JSF) JDBC Servlet JSP Java Ajax Dojo JavaScript HTML XML PHP Core PHP Advance MySQL Interview Questions

Advertise With Us

Previous

Home

Next

Hibernate First Example


In this section, you will learn how to set environment in eclipse for hibernte application. Follows the following steps: Step 1: Open eclipse and select the file menu then open the following options. You choose:

Step 2: Click on Java Project then open the following:

Step 3: Click on "Finish" command button. Then your project is created in package explorer. Select your project and follows the following figure:

Step 4: Choose Folder opetion.

Step 5: Then lib folder is created under the project. Copy all hibernate library and paste in your lib folder then it will display:

Step 6: Configure the build path:

Step 7: Click on Add Jar command button then open a dialog box and select all jars:

Step 8: Click on Ok command button:

Step 9: Again click on Ok button. Create a hibernate.cfg.xml file:

<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/ hibernate-configuration-3.0.dtd">

<hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class"> com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">

jdbc:mysql://localhost/hibernateExamples</property> <property name="hibernate.connection.username"> root</property> <property name="hibernate.connection.password"> </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="employee.hbm.xml"/> </session-factory> </hibernate-configuration>
Step 10: Create employee.hbm.xml file:

<?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="developerhelpway.hibernate.Employee" table="employee"> <id name="empId" type="int" column="emp_id" > <generator class="increment"/> </id> <property name="empName"> <column name="emp_name" /> </property> <property name="empSal"> <column name="emp_sal" /> </property>

</class> </hibernate-mapping> Step 11: Create a class:

Step 12: Create required fields:

public classEmployee { private intempId; private StringempName; private intempSal; } Step 13: To create auto generator through the eclipse:

Step 14:Open the following dialog:

Step 15: Then you get auto generate setter and getter methods:

packagedeveloperhelpway.hibernate; public class Employee { private int empId; private String empName; private int empSal; public int getEmpId() { return empId; }

public void setEmpId(intempId) { this.empId = empId; } public String getEmpName() { return empName; } public voidsetEmpName(String empName) { this.empName = empName; } public int getEmpSal() { return empSal; } public void setEmpSal(intempSal) { this.empSal = empSal; } } Step 16: Now, you create main class: FirstExample.java package developerhelpway.hibernate; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; public class FirstExample { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Session sess = null; Transaction tran = null; try{ SessionFactory sessFact = new Configuration().configure().buildSessionFactory(); sess = sessFact.openSession(); System.out.println("Session: "+ sess); tran = sess.beginTransaction();

Employee emp = new Employee(); emp.setEmpName("Birendra Kumar"); emp.setEmpSal(12000); sess.save(emp); tran.commit(); } catch(Exception ex){ ex.printStackTrace(); } finally{ sess.close(); } }
}

To run this application follows the following instructions: Go and select the following:

Then open the following dialog:

Click on Ok command button: Output:

Download Example

Previous
E-mail Account
Create Your Account Login:

Home

Next

Password:

Remember me
LOGIN

Password forgotten?

Good Programming Tips Advertise Here


Advertise with Us Add Place Copyright 2009 Developerhelpway.com Out Portal edufever.com

You might also like