Hibernate practical tutorial

Post on 15-May-2015

728 views 5 download

Tags:

Transcript of Hibernate practical tutorial

Hibernate Practical Tutorial

System Requirements

1. Eclipse and MyEclipse(I have used MyEclipse 5.5) installed on your computer.2. mySql installed3. My sql connector jar file( e.g. I have mysql-connector-java-3.1.12-bin)4. Any client software installed (e.g. I have SQLyog Enterprise 4)5. 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 test…and 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 junaid.rehman@yahoo.com