You are on page 1of 27

CHUYN JAVA

GII THIU HIBERNATE


Nguyn Hong Anh Email: nhanh@fit.hcmus.edu.vn hoanganhis@gmail.com H KHTN, 2011

Upload by Cafeitvn.com

Ni dung trnh by
Gii thiu Hibernate

Xy dng ng dng qun l sinh vin Hibernate


Ly danh sch sinh vin

n gin vi

Ly thng tin sinh vin


Thm sinh vin Cp nht sinh vin

Xa sinh vin

Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2011

Upload by Cafeitvn.com

Hibernate
C s d liu thng c thit k v lu tr theo hng quan h. Tuy nhin phn mm thng c xy dng theo hng i tng.

i vi lp trnh vin khi xy dng phn mm thng mun lm vic vi cc i tng v khng phi nh n cc dng , cc ct trong cc bng ca c s d liu.

Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2011

Upload by Cafeitvn.com

Ci t

Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2011

Upload by Cafeitvn.com

Kin trc Hibernate


Code
POJOs

Mappings

Hibernate

Configuaration

JDBC

DATABASE

Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2011

Upload by Cafeitvn.com

Cc bc s dng hibernate
Bc 1: To c s d liu

Bc 2: To cc POJO
Bc 3: To file cu hnh hibernate.cfg.xml Bc 4: To cc file mapping <POJO>.hbm.xml Bc 5: Khai bo cc file mapping vo hibernate.cfg.xml Bc 6: Xy dng lp HibernateUtil

Bc 7: Xy dng cc DAO & S dng

Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2011

Upload by Cafeitvn.com

Bc 1: To c s d liu
CSDL: MySQL QuanLySinhVien

Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2011

Upload by Cafeitvn.com

Bc 2: To POJO SinhVien
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
8

package pojo; import java.util.Date; public class SinhVien implements java.io.Serializable { private String maSinhVien; private String hoVaTen; private Date ngaySinh; private String diaChi; public SinhVien() { } public SinhVien(String maSinhVien) { this.maSinhVien = maSinhVien; } public SinhVien(String maSinhVien, String hoVaTen, Date ngaySinh, String diaChi) { this.maSinhVien = maSinhVien; this.hoVaTen = hoVaTen; this.ngaySinh = ngaySinh; this.diaChi = diaChi; }
Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2011

Upload by Cafeitvn.com

Bc 2: To POJO SinhVien
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 public String getMaSinhVien() { return this.maSinhVien; } public void setMaSinhVien(String maSinhVien) { this.maSinhVien = maSinhVien; } public String getHoVaTen() { return this.hoVaTen; } public void setHoVaTen(String hoVaTen) { this.hoVaTen = hoVaTen; } public Date getNgaySinh() { return this.ngaySinh; } public void setNgaySinh(Date ngaySinh) { this.ngaySinh = ngaySinh; } public String getDiaChi() { return this.diaChi; } public void setDiaChi(String diaChi) { this.diaChi = diaChi; }

Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2011

Upload by Cafeitvn.com

Bc 3: To file cu hnh hibernate.cfg.xml


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration3.0.dtd"> <hibernate-configuration> <session-factory> . . . </session-factory> </hibernate-configuration>

10

Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2011

Upload by Cafeitvn.com

Bc 3: To file cu hnh hibernate.cfg.xml


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
<session-factory> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.connection.driver_class"> com.mysql.jdbc.Driver </property> <property name="hibernate.connection.url"> jdbc:mysql://localhost:3306/QuanLySinhVien?autoReconnect&amp; useUnicode=true&amp;characterEncoding=UTF-8 </property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">root</property> <mapping resource="pojo/SinhVien.hbm.xml"/> <!Mapping Resources--> </session-factory>

11

Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2011

Upload by Cafeitvn.com

Bc 4: To cc file mapping SinhVien.hbm.xml


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> ... </hibernate-mapping>

12

Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2011

Upload by Cafeitvn.com

Bc 4: To file mapping SinhVien.hbm.xml


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 <class catalog="quanlyhocsinh" name="pojo.SinhVien" table="sinhvien"> <id name="maSinhVien" type="string"> <column length="7" name="MaSinhVien"/> <generator class="assigned"/> </id> <property name="hoVaTen" type="string"> <column length="45" name="HoVaTen"/> </property> <property name="ngaySinh" type="date"> <column length="10"name="NgaySinh"/> </property> <property name="diaChi" type="string"> <column length="45" name="DiaChi"/> </property> </class>
Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2011

13

Upload by Cafeitvn.com

Bc 5: Khai bo mapping vo hibernate.cfg.xml


1 2 3 4 5 6 7 8 9 10 11 12 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration3.0.dtd"> <hibernate-configuration> <session-factory> <!Property--> ... <mapping resource="pojo/SinhVien.hbm.xml"/> </session-factory> </hibernate-configuration>

14

Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2011

Upload by Cafeitvn.com

Bc 6: Xy dng lp HibernateUtil
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 import org.hibernate.cfg.AnnotationConfiguration; import org.hibernate.SessionFactory; public class HibernateUtil { private static final SessionFactory sessionFactory; static { try { sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory(); } catch (Throwable ex) { System.err.println("Initial SessionFactory creation failed." + ex); throw new ExceptionInInitializerError(ex); } } public static SessionFactory getSessionFactory() { return sessionFactory; } }

15

Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2011

Upload by Cafeitvn.com

Bc 7 : Xy dng lp SinhVienDAO & s dng


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 public class SinhVienDAO { public static List<SinhVien> layDanhSachSinhVien() { List<SinhVien> ds = null; Session session = HibernateUtil.getSessionFactory() .openSession(); try { String hql = "select sv from SinhVien sv"; Query query = session.createQuery(hql); ds = query.list(); } catch (HibernateException ex) { //Log the exception System.err.println(ex); } finally { session.close(); } return ds; } . . .

16

Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2011

Upload by Cafeitvn.com

Bc 7 : Xy dng lp SinhVienDAO & s dng


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 public class Main { public static void main(String[] args) { //<editor-fold defaultstate="collapsed" desc="1. Ly danh sch sinh vin"> List<SinhVien> ds=SinhVienDAO.layDanhSachSinhVien(); for(int i=0; i<ds.size(); i++){ SinhVien sv=ds.get(i); System.out.println("MSSV: "+sv.getMaSinhVien()); System.out.println("H v tn: "+sv.getHoVaTen()); System.out.println("Ngy sinh: " + sv.getNgaySinh()); System.out.println("a ch: "+ sv.getDiaChi()); } //</editor-fold>

17

Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2011

Upload by Cafeitvn.com

Bc 7 : Xy dng lp SinhVienDAO & s dng


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 public class SinhVienDAO { public static SinhVien layThongTinhSinhVien(String maSinhVien) { SinhVien sv = null; Session session = HibernateUtil.getSessionFactory() .openSession(); try { sv = (SinhVien) session.get(SinhVien.class, maSinhVien); } catch (HibernateException ex) { //Log the exception System.err.println(ex); } finally { session.close(); } return sv; } . . .

18

Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2011

Upload by Cafeitvn.com

Bc 7 : Xy dng lp SinhVienDAO & s dng


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
19

public class Main { public static void main(String[] args) { //<editor-fold defaultstate="collapsed" desc="2. Ly thng tin sinh vin"> SinhVien sv = SinhVienDAO.layThongTinSinhVien("0312143"); if(sv!=null){ System.out.println("MSSV: " + sv.getMaSinhVien()); System.out.println("H v tn: " + sv.getHoVaTen()); System.out.println("Ngy sinh: " + sv.getNgaySinh()); System.out.println("a ch: " + sv.getDiaChi()); }else{ System.out.println("Sinh vin 0312143 khng tn ti"); } //</editor-fold>
Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2011

Upload by Cafeitvn.com

Bc 7 : Xy dng lp SinhVienDAO & s dng


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 public class SinhVienDAO { public static boolean themSinhVien(SinhVien sv) { Session session = HibernateUtil.getSessionFactory().openSession(); if (SinhVienDAO.layThongTinSinhVien(sv.getMaSinhVien())!=null) { return false; } Transaction transaction = null; try { transaction = session.beginTransaction(); session.save(sv); transaction.commit(); } catch (HibernateException ex) { //Log the exception transaction.rollback(); System.err.println(ex); } finally { session.close(); } return true; }

20

Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2011

Upload by Cafeitvn.com

Bc 7 : Xy dng lp SinhVienDAO & s dng


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
21

public class Main { public static void main(String[] args) { //<editor-fold defaultstate="collapsed" desc="3. Thm sinh vin"> SinhVien sv = new SinhVien(); sv.setMaSinhVien("0312171"); sv.setHoVaTen("T Tn Thm"); Calendar calendar = Calendar.getInstance(); calendar.set(1985, 5, 18); Date d = calendar.getTime(); sv.setNgaySinh(d); sv.setDiaChi("Vng Lim Vnh Long"); boolean kq = SinhVienDAO.themSinhVien(sv); if (kq == true) { System.out.println("Thm thnh cng"); } else { System.out.println("Thm tht bi"); } //</editor-fold>
Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2011

Upload by Cafeitvn.com

Bc 7 : Xy dng lp SinhVienDAO & s dng


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 public class SinhVienDAO { public static boolean capNhatThongTinSinhVien(SinhVien sv) { Session session = HibernateUtil.getSessionFactory().openSession(); if (SinhVienDAO.layThongTinSinhVien(sv.getMaSinhVien()) == null) { return false; } Transaction transaction = null; try { transaction = session.beginTransaction(); session.update(sv); transaction.commit(); } catch (HibernateException ex) { //Log the exception transaction.rollback(); System.err.println(ex); } finally { session.close(); } return true; } . . .

22

Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2011

Upload by Cafeitvn.com

Bc 7 : Xy dng lp SinhVienDAO & s dng


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
23

public class Main { public static void main(String[] args) { //<editor-fold defaultstate="collapsed" desc="4. Cp nht sinh vin"> SinhVien sv = SinhVienDAO.layThongTinSinhVien("0312143"); if (sv != null) { sv.setHoVaTen("T Tn Thm"); Calendar calendar = Calendar.getInstance(); calendar.set(1985, 5, 18); Date d = calendar.getTime(); sv.setNgaySinh(d); sv.setDiaChi("Vng Lim Vnh Long"); boolean kq = SinhVienDAO.capNhatThongTinSinhVien(sv); if (kq == true) { System.out.println("Cp nht thnh cng"); } else { System.out.println("Cp nht tht bi"); } } //</editor-fold>
Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2011

Upload by Cafeitvn.com

Bc 7 : Xy dng lp SinhVienDAO & s dng


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
24

public class SinhVienDAO { public static boolean xoaSinhVien(String maSinhVien) { Session session = HibernateUtil.getSessionFactory().openSession(); SinhVien sv = SinhVienDAO.layThongTinSinhVien(maSinhVien); if(sv==null){ return false; } Transaction transaction = null; try { transaction = session.beginTransaction(); session.delete(sv); transaction.commit(); } catch (HibernateException ex) { //Log the exception transaction.rollback(); System.err.println(ex); } finally { session.close(); } return true; } }
Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2011

Upload by Cafeitvn.com

Bc 7 : Xy dng lp SinhVienDAO & s dng


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 public class Main { public static void main(String[] args) { //<editor-fold defaultstate="collapsed" desc="5. Xa sinh vin"> boolean kq = SinhVienDAO.xoaSinhVien("0312143"); if (kq == true) { System.out.println("Xa thnh cng"); } else { System.out.println("Xa tht bi"); } } //</editor-fold> } }

25

Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2011

Upload by Cafeitvn.com

Ti liu tham kho


Nguyn Hong Anh, Tp bi ging v video mn chuyn Java, 2010 Marty Hall, Tp bi ging v Servlet, 2010 http://courses.coreservlets.com/Course-Materials/csajsp2.html

26

Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2011

Upload by Cafeitvn.com

HI V P
Upload by Cafeitvn.com

27

Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2011

You might also like