You are on page 1of 27

GII THIU HIBERNATE

CHUYN JAVA
Nguyn Hong Anh
Email: nhanh@fit.hcmus.edu.vn
hoanganhis@gmail.com
H KHTN, 2011
Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2011
Ni dung trnh by
Gii thiu Hibernate
Xy dng ng dng qun l sinh vin n gin vi
Hibernate
Ly danh sch sinh vin
Ly thng tin sinh vin
Thm sinh vin
Cp nht sinh vin
Xa sinh vin
2
Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2011
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.
3
Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2011
Ci t

4
Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2011
Kin trc Hibernate
5
Code
Hibernate
Mappings
Configuaration
POJOs
JDBC
DATABASE
Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2011
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

6
Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2011
Bc 1: To c s d liu
7
CSDL: MySQL
QuanLySinhVien
Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2011
Bc 2: To POJO SinhVien
8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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;
}
}
Bc 2: To POJO SinhVien
9
Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2011
Bc 3: To file cu hnh hibernate.cfg.xml
10
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-configuration-
3.0.dtd">
<hibernate-configuration>
<session-factory>
. . .
</session-factory>
</hibernate-configuration>


Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2011
Bc 3: To file cu hnh hibernate.cfg.xml
11
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>
Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2011
Bc 4: To cc file mapping SinhVien.hbm.xml
12
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>

Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2011
Bc 4: To file mapping SinhVien.hbm.xml
13
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
Bc 5: Khai bo mapping vo hibernate.cfg.xml
14
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-configuration-
3.0.dtd">
<hibernate-configuration>
<session-factory>
<!Property-->
...
<mapping resource="pojo/SinhVien.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2011
Bc 6: Xy dng lp HibernateUtil
15
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;
}
}
Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2011
Bc 7 : Xy dng lp SinhVienDAO & s dng
16
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;
}
. . .
Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2011
Bc 7 : Xy dng lp SinhVienDAO & s dng
17
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>
Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2011
Bc 7 : Xy dng lp SinhVienDAO & s dng
18
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;
}
. . .
Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2011
Bc 7 : Xy dng lp SinhVienDAO & s dng
19
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
Bc 7 : Xy dng lp SinhVienDAO & s dng
20
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;
}
Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2011
Bc 7 : Xy dng lp SinhVienDAO & s dng
21
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
Bc 7 : Xy dng lp SinhVienDAO & s dng
22
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;
}
. . .
Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2011
Bc 7 : Xy dng lp SinhVienDAO & s dng
23
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
Bc 7 : Xy dng lp SinhVienDAO & s dng
24
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
Bc 7 : Xy dng lp SinhVienDAO & s dng
25
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>
}
}
Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2011
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
HI V P
27

You might also like