hibernate practical tutorial
TRANSCRIPT
![Page 1: Hibernate Practical Tutorial](https://reader033.vdocuments.net/reader033/viewer/2022051616/553d311b550346252e8b4569/html5/thumbnails/1.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022051616/553d311b550346252e8b4569/html5/thumbnails/2.jpg)
![Page 3: Hibernate Practical Tutorial](https://reader033.vdocuments.net/reader033/viewer/2022051616/553d311b550346252e8b4569/html5/thumbnails/3.jpg)
![Page 4: Hibernate Practical Tutorial](https://reader033.vdocuments.net/reader033/viewer/2022051616/553d311b550346252e8b4569/html5/thumbnails/4.jpg)
![Page 5: Hibernate Practical Tutorial](https://reader033.vdocuments.net/reader033/viewer/2022051616/553d311b550346252e8b4569/html5/thumbnails/5.jpg)
![Page 6: Hibernate Practical Tutorial](https://reader033.vdocuments.net/reader033/viewer/2022051616/553d311b550346252e8b4569/html5/thumbnails/6.jpg)
Create New Project
Create a new Java Project and name it HibernateProject2.
![Page 7: Hibernate Practical Tutorial](https://reader033.vdocuments.net/reader033/viewer/2022051616/553d311b550346252e8b4569/html5/thumbnails/7.jpg)
Include Hibernate capabilities.
![Page 8: Hibernate Practical Tutorial](https://reader033.vdocuments.net/reader033/viewer/2022051616/553d311b550346252e8b4569/html5/thumbnails/8.jpg)
![Page 9: Hibernate Practical Tutorial](https://reader033.vdocuments.net/reader033/viewer/2022051616/553d311b550346252e8b4569/html5/thumbnails/9.jpg)
![Page 10: Hibernate Practical Tutorial](https://reader033.vdocuments.net/reader033/viewer/2022051616/553d311b550346252e8b4569/html5/thumbnails/10.jpg)
![Page 11: Hibernate Practical Tutorial](https://reader033.vdocuments.net/reader033/viewer/2022051616/553d311b550346252e8b4569/html5/thumbnails/11.jpg)
Click on finish.
![Page 12: Hibernate Practical Tutorial](https://reader033.vdocuments.net/reader033/viewer/2022051616/553d311b550346252e8b4569/html5/thumbnails/12.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022051616/553d311b550346252e8b4569/html5/thumbnails/13.jpg)
![Page 14: Hibernate Practical Tutorial](https://reader033.vdocuments.net/reader033/viewer/2022051616/553d311b550346252e8b4569/html5/thumbnails/14.jpg)
![Page 15: Hibernate Practical Tutorial](https://reader033.vdocuments.net/reader033/viewer/2022051616/553d311b550346252e8b4569/html5/thumbnails/15.jpg)
HQL Editor
Right click on the project Hibernate Project2 and go to myEclipse and select open HQL Editor.
![Page 16: Hibernate Practical Tutorial](https://reader033.vdocuments.net/reader033/viewer/2022051616/553d311b550346252e8b4569/html5/thumbnails/16.jpg)
![Page 17: Hibernate Practical Tutorial](https://reader033.vdocuments.net/reader033/viewer/2022051616/553d311b550346252e8b4569/html5/thumbnails/17.jpg)
![Page 18: Hibernate Practical Tutorial](https://reader033.vdocuments.net/reader033/viewer/2022051616/553d311b550346252e8b4569/html5/thumbnails/18.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022051616/553d311b550346252e8b4569/html5/thumbnails/19.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022051616/553d311b550346252e8b4569/html5/thumbnails/20.jpg)
+", 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]