0% found this document useful (0 votes)
301 views

Hibernate Practical Tutorial

The document provides steps to set up Hibernate with MySQL using Eclipse and MyEclipse. It includes instructions to install required software, create a database table, generate Hibernate files from the table, and write and run a sample program to perform CRUD operations on a 'User' object using Hibernate. The program adds a new user, retrieves an existing user, updates the user details, and prints the users to confirm the changes.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
301 views

Hibernate Practical Tutorial

The document provides steps to set up Hibernate with MySQL using Eclipse and MyEclipse. It includes instructions to install required software, create a database table, generate Hibernate files from the table, and write and run a sample program to perform CRUD operations on a 'User' object using Hibernate. The program adds a new user, retrieves an existing user, updates the user details, and prints the users to confirm the changes.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 20

Hibernate Practical Tutorial

System Requirements 1. 2. 3. 4. 5. Eclipse and MyEclipse(I have used MyEclipse 5.5) installed on your computer. mySql installed My sql connector jar file( e.g. I have mysql-connector-java-3.1.12-bin) Any client software installed (e.g. I have SQLyog Enterprise 4) Jdk1.5

Create table When you install SQLyog, test database exists as default when you login through root as your name. Create table named user in test. It has following colums: 1. id(int)(primary key)(not null) 2. username(varchar)(lenth=50) 3. password(varchar) (lenth=50) 4. firstname(varchar) (lenth=50) 5. lastname(varchar) (lenth=50) 6. datecreated(varchar) (lenth=50) [you may also use any other data type..] MyEclipse Database Explorer Open MyEclipse Database Explorer from window->open perspective. In DB Browser right click and select new. Follow the steps visible in screen shot. Click Add Jars and select the location of My sql connector jar file( e.g. I have mysql-connector-java-3.1.12-bin) Click on Finish.

Create New Project Create a new Java Project and name it HibernateProject2.

Include Hibernate capabilities.

Click on finish.

Reverse Engineer the DB Table Open the DB Browser. Right click on newly created DriverName(the just created above) and click open connection. Right click on the table user in the testand select hibernate reverse engineering.

HQL Editor Right click on the project Hibernate Project2 and go to myEclipse and select open HQL Editor.

Create java class HibernateExample.java

Write the following code in the HibernateExample.java and run HibernateExample.java as java application..
package com.myeclipse.hibernate; import org.hibernate.Transaction; public class HibernateExample { /** * @param args */ public static void main(String[] args) { addUser(); listUser(); changeUser(); } public static void addUser() { User user = new User(); user.setId(2);

user.setUsername("1234"); user.setPassword("1234"); user.setFirstname("Junaid"); user.setLastname("Rehman"); user.setDatecreated("12-2-2009"); UserDAO dao = new UserDAO(); Transaction tx = dao.getSession().beginTransaction(); dao.save(user); tx.commit(); dao.getSession().close(); } private static void listUser() { UserDAO dao = new UserDAO(); User user = dao.findById(1); printUser("Printing User, ", user); dao.getSession().close(); } private static void changeUser() { UserDAO dao = new UserDAO(); User user = dao.findById(1); user.setUsername("5678"); user.setPassword("5678"); user.setFirstname("bilal"); user.setLastname("bilal"); Transaction tx = dao.getSession().beginTransaction(); dao.save(user); tx.commit(); User updatedUser = dao.findById(1); printUser("Printing Updated User, ", updatedUser); dao.getSession().close(); } private static void printUser(String extraText, User user){ System.out.println(extraText +"User[ Username: " +user.getUsername() +", Password: " +user.getPassword() +", Firstname: " +user.getFirstname()

+", Lastname: " +user.getLastname()+"]");

} }

Here is the output:


Printing User, User[ Username: 5678, Password: 5678, Firstname: bilal, Lastname: bilal] Printing Updated User, User[ Username: 5678, Password: 5678, Firstname: bilal, Lastname: bilal]

Now you can open HQL Editor again and write from User and see the result..

Resource Website:http://www.myeclipseide.com/documentation/quickstarts/hibernateintroduction/ Contact Me:- If you have any arguments you can send me email at [email protected]

You might also like