Professional Documents
Culture Documents
HIBERNATE MAPPING
ONE-TO-MANY
Nguyn Hong Anh Email: nhanh@fit.hcmus.edu.vn H KHTN, 2011
Upload by Cafeitvn.com
Ni dung trnh by
One to many
Lazy Initialization
Fetch Cascade
Upload by Cafeitvn.com
Upload by Cafeitvn.com
Upload by Cafeitvn.com
BookOnline
DanhMuc
1 *
Sach - maSach - tenSach - tacGia - giaBan - soLuong - hinhAnh - danhMuc :String :String :String :double :int :String :DanhMuc
Upload by Cafeitvn.com
DanhMuc
Sach
1 *
implements java.io.Serializable {
private String maDanhMuc; private String tenDanhMuc; private Set<Sach> sachs = new HashSet<Sach>(0);
//Cc phng thc khi to . . . //Getter & Setter . . .
Upload by Cafeitvn.com
DanhMuc.hbm.xml - Set
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
DanhMuc
1 *
Sach - maSach - tenSach - tacGia - giaBan - soLuong - hinhAnh - danhMuc :String :String :String :double :int :String :DanhMuc
<hibernate-mapping> <class name="pojo.DanhMuc" table="danhmuc"> <id name="maDanhMuc" type="string"> <column length="45" name="MaDanhMuc"/> <generator class="assigned"/> </id> <property name="tenDanhMuc" type="string"> <column length="45" name="TenDanhMuc" not-null="true"/> </property> <set inverse="true" name="sachs" > <key> <column length="45" name="MaDanhMuc" not-null="true"/> </key> <one-to-many class="pojo.Sach"/> </set> </class> </hibernate-mapping>
Upload by Cafeitvn.com
Sach POJO
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
DanhMuc
1 *
Sach - maSach - tenSach - tacGia - giaBan - soLuong hinhAnh {- danhMuc :String :String :String :double :int :String :DanhMuc
package pojo; public class Sach implements java.io.Serializable private String maSach; private DanhMuc danhMuc; private String tenSach; private String tacGia; private double giaBan; private int soLuong; private String hinhAnh;
//Constructors
//Getters & Setters
Upload by Cafeitvn.com
Sach.hbm.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 <hibernate-mapping> <class catalog="bookonline" name="pojo.Sach" table="sach"> <id name="maSach" type="string"> <column length="45" name="MaSach"/> <generator class="assigned"/> </id> . . . <many-to-one class="pojo.DanhMuc" name="danhMuc" fetch="select"> <column length="45" name="MaDanhMuc" not-null="true"/> </many-to-one> </class> </hibernate-mapping>
DanhMuc
1 *
Sach - maSach - tenSach - tacGia - giaBan - soLuong - hinhAnh - danhMuc :String :String :String :double :int :String :DanhMuc
Upload by Cafeitvn.com
10
Upload by Cafeitvn.com
11
Upload by Cafeitvn.com
Lazy = true
12
Upload by Cafeitvn.com
Lazy Initialization
Trong Hibernate, Lazy Initialization gip
Lazy mc nh c gi tr l true
13
Upload by Cafeitvn.com
DanhMuc.hbm.xml - Set
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
DanhMuc
1 *
Sach - maSach - tenSach - tacGia - giaBan - soLuong - hinhAnh - danhMuc :String :String :String :double :int :String :DanhMuc
<hibernate-mapping> <class name="pojo.DanhMuc" table="danhmuc"> <id name="maDanhMuc" type="string"> <column length="45" name="MaDanhMuc"/> <generator class="assigned"/> </id> <property name="tenDanhMuc" type="string"> <column length="45" name="TenDanhMuc" not-null="true"/> </property> <set inverse="true" name="sachs" fetch="join" lazy="false" > <key> <column length="45" name="MaDanhMuc" not-null="true"/> </key> <one-to-many class="pojo.Sach"/> </set> </class> </hibernate-mapping>
14
Upload by Cafeitvn.com
15
Upload by Cafeitvn.com
Lazy = false
16
Upload by Cafeitvn.com
DanhMuc.hbm.xml - Set
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
DanhMuc
1 *
Sach - maSach - tenSach - tacGia - giaBan - soLuong - hinhAnh - danhMuc :String :String :String :double :int :String :DanhMuc
<hibernate-mapping> <class name="pojo.DanhMuc" table="danhmuc"> <id name="maDanhMuc" type="string"> <column length="45" name="MaDanhMuc"/> <generator class="assigned"/> </id> <property name="tenDanhMuc" type="string"> <column length="45" name="TenDanhMuc" not-null="true"/> </property> <set inverse="true" name="sachs" > <key> <column length="45" name="MaDanhMuc" not-null="true"/> </key> <one-to-many class="pojo.Sach"/> </set> </class> </hibernate-mapping>
17
Upload by Cafeitvn.com
public class DanhMucDAO { public static DanhMuc layThongTinDanhMuc(String maDanhMuc) { DanhMuc dm = null; Session session = HibernateUtil.getSessionFactory() .openSession(); try { String hql=" select dm "; hql+= " from DanhMuc dm left join fetch dm.sachs"; hql+= " where dm.maDanhMuc=:maDanhMuc"; Query query=session.createQuery(hql); query.setString("maDanhMuc", maDanhMuc); dm=(DanhMuc)query.uniqueResult(); } catch (HibernateException ex) { System.err.println(ex); } finally { session.close(); } return dm; }
Nguyn Hong Anh nhanh@fit.hcmus.edu.vn H KHTN - 2011
Upload by Cafeitvn.com
19
Upload by Cafeitvn.com
HQL
20
Upload by Cafeitvn.com
Cascade
save-update
delete
delete-orphan
21
Upload by Cafeitvn.com
22
Upload by Cafeitvn.com
HI V P
Upload by Cafeitvn.com
23