Hibernate practical tutorial

21
Hibernate Practical Tutorial System Requirements 1. Eclipse and MyEclipse(I have used MyEclipse 5.5) installed on your computer. 2. mySql installed 3. 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)

Transcript of Hibernate practical tutorial

Page 1: 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.

Page 2: Hibernate practical tutorial
Page 3: Hibernate practical tutorial
Page 4: Hibernate practical tutorial
Page 5: Hibernate practical tutorial
Page 6: Hibernate practical tutorial

Create New Project

Create a new Java Project and name it HibernateProject2.

Page 7: Hibernate practical tutorial

Include Hibernate capabilities.

Page 8: Hibernate practical tutorial
Page 9: Hibernate practical tutorial
Page 10: Hibernate practical tutorial
Page 11: Hibernate practical tutorial

Click on finish.

Page 12: Hibernate practical tutorial

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.

Page 13: Hibernate practical tutorial
Page 14: Hibernate practical tutorial
Page 15: Hibernate practical tutorial

HQL Editor

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

Page 16: Hibernate practical tutorial
Page 17: Hibernate practical tutorial
Page 18: Hibernate practical tutorial

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);

Page 19: Hibernate practical tutorial

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()

Page 20: Hibernate practical tutorial

+", 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]