You are on page 1of 6

http://www.developer.com/open/article.php/3559931/Hibernate-Basics.htm Mapping gia mt class/entity vi mt table trong database.

C hai cch : - Dng java annotation : s dng cc annotation ngay trong class v d : @entity , @id . - Dng xml file mappping : mi class c map tng ng vi mt Class Element Mi xml file c th dng map nhiu class nhiu Class Element tuy nhin ta thng dng mt XML file map tng ng cho mt class ( v : bo tr , r rng, g ri. ). Cc XML File mapping c t tn theo naming convention l tn class cng vi phn m rng l hbn.xml: {Class_name}.hbn.xml Cc vn quan tm khi thc hin mapping l : - Mi quan h gia cc Class/entity - Primary key & foreign key :sinh kha, m ha kha ,t tng , s dng sequence . - Name map m hibernate s dng v name ca cc table v column . - Join

Cch dng XML File


mapping mt persistent class s tng ng dng class element t trong hibernate-mapping element
<hibernate-mapping package="com.sample">

<class name="com.sample.Message" table="message">


Vi hai attribute thng dng l name & table attribute. Mt im lu l package attribute ca hibernate-mapping element ch ra Location/package m cc class ( cc class element map ti cc class ) c th t vo bn trong hibernate-mapping. tc l ch c cc class no bn trong package th mi c th mapping trong file ny. Li im th hai l trnh xung t tn cc class khi hibernate map chng , mc nh nu khng ch ra thuc tnh package ny th hibernate s map cc class ny vo default package nh vy nu hai tp mapping c cha hai class ging tn th MappingException s c throw do vy ta nn s dng thuc tnh ny Mt cch n gin th gi tr ca package attribute chnh l package tng ng m persistent class cn mapping ang c tr.

1. Map cc property ca Persistent class


ID & generators
Mt persistent class phi c mt Pramiry key( tng ng primary key ca table ) v tng ng th hin bi mt id element khi mapping. V d
<id

name="id" column="uid" type="long" unsaved-value="null">

<generator class="native"/> </id>

Name attribute : tng ng vi property ca persistent class Column attribute : tng ng vi column name ca table. Nu khng ch ra thuc tnh column th mc nh n nhn gi tr ca thuc tnh name . unsave-value attribute : Generator element : ch ra cc thuc tnh lin quan ti key thng qua class attribute : t tng..
Figure 1 Class mapping

Mi gi tr class attribute ca Generator element tng ng l mt class m hibernate cung cp sinh ra primary key. Mi class ny s c mt cch ci t khc nhau sinh primary key. Di y l mt s class v c im ca chng. - Assigned : sinh ra v gn Object ID cho php ng dng s dng li legacy code , v d nh uuid genertor t mt EJB application. - Uuid : - Native : cho php sinh primary key linh ng da trn nhng support DB h tr, c th hn native class s s dng identity or sequence columns ty thuc vo DB sinh kha. Nu c hai phng thc trn ( dng identify or sequence column ) u khng c support th n s to ra unique primary key values. Databases supporting identity columns include Sybase, MySQL, Microsoft SQL Server, and IBM DB2 Oracle, PostgreSQL, and SAP DB support sequence columns. NOTE : The native generator returns a short, integer, or long value do vy ta nn t id l kiu java.lang.Long ( ch l full class ti Long ).

Properties
Mapping cho cc properties ca persistent classs S dng property element vi cc attribute thng dng l : name type column attribute Ngoi cc attribute trn cn kh nhiu thuc tnh khc m ngha ca n rt gn vi cc constraint trong DB V d : <property name="sdf" column="" type="" not-null="true" unique="false" unique-key="" index=""> </property> Nhc li : name attribute tng ng vi property ca persistent class , cn column attribute tng ng vi tn ca column trong DB Table. Mc nh name attribute cng ch ra column tng ca table , nhng nu column attribute c ch ra th n s override name attribute if the type attribute is omitted, Hibernate determines the type using runtime reflection. In certain cases, you must provide the property type, since reflection may not be able to determine the desired type (such as differentiating between the Hibernate DATE and TIMESTAMP types). Valid property types include the Hibernate basic types, such as integer, string, and timestamp, as well as the corresponding Java objects and primitives. However, you aren't limited to basic data types. The property element may also contain the name of a serializable Java class or a user-defined type. You create a new user-defined type by implementing either the org.hibernate.UserType or org.hibernate. CompositeUserType interface. The fully qualified class name of the user type or the serializable Java class is used as the property type value.

2. Map quan h gia cc persistent class


mapping cc quan h gia cc persistent class cng s c cc element tng ng. 3 quan h gia hai class bt k : many-to-many( bng trung gian lu hai primary key ca hai table ) , many-to-one ( priamry key pha one c t bn pha many ) , one-to-one ( priamry key t c hai bn , key ca table ny t bn table ka v ngc li ) Trong hibernate t khi dng one- to one m dng many-to-one thay th .

Many-to-one element:

Figure 2 many-to-one relationship

Quan h trn vi mi tn mt chiu ch ra rng nhiu Event object c th thuc mt Location object. Vi quan h Many-to-one ta ch cn a thm many-to-one element vo mapping file ca class pha many trong quan h. V d vi quan h hnh trn th mapping file ca Location vn nh thng l , ch c Event phi a thm many-to-one element nh sau .

<many-to-one name="location"column="location_id"class="Location"/>
Trong : - Thuc tnh class ch ra tng ng persistent class ( y l Location class ) trong quan h - class pha one . - Thuc tnh name : ch ra property/var trong class pha bn many tng ng tham chiu class pha one ( tc y tham chiu ti mt Location object) hay d hiu hn y chnh l name t cho foreign key . - Thuc tnh Column : ch ra column ca table tng ng vi persistent class ( pha many ) s c dng lu foreign key , tc lu gi tr ca thuc tnh name c ch ra trn Ch : nu thuc tnh class tham chiu ti mt class nm ngoi package vi class map hin thi th phi ch ra fully qualified name ca class . Nhn thy nu object cha c ly ( obejct pha nhiu ) th object lin kt cng s c ly v ( object pha one ) . Nu mun ly object cha m khng mun nhn v cc object con th ? c gi l to ra many-to-one relationship lazy .
how to make a many-to-one relationship lazy :

ly object cha m khng ly cc object lin kt

gii php : s dng proxies hibernate cho php s dng mt object proxy trnh nhn v cc object ( cc object lin kt ) cho n khi thy cn thit . note : mc nh hibernate 2 khng support proxy mt cch mc nh m c th s dng nh l preferred. Trong hibernate 3 th proxy c support nh mc nh. Object proxy c nh ngha theo 2 cch sau : - S dng thuc tnh proxy trong class element : c th ch ra mt persistent class hoc mt class bt k .
V d : <class name="Location" proxy="com.manning.hq.ch03.Location"...>... </class>

S dng thuc tnh lazy : thit lp lazy=true , l mt cch ngn gn shorthand mt persistent class nh l mt proxy. V d : ta define Location class l mt lazy
<class name="Location"lazy="true"...>...</class>

NOTE :Ttrong hibernate 3 th mc nh lazy=true s , ta cng c th disable ton b proxies ny thng qua thuc tnh default-lazy ca hibernate-mapping element . V d
<hibernate-mapping default-lazy="false" >

V d :s dng proxy
// s dng Location lazy Session session =factory.openSession(); Event ev =(Event)session.load(Event.class,myEventId); Location loc =ev.getLocation(); String name =loc.getName(); session.close();

xem xt v d trn , khi gi load() method th ch c instance ca Event c to ra : bao gm cc property ca Event cng vi ID ca Location ( tc foreign key ) , m cinstance ca Location cha c to ra. Instance ca Location lazy ch c to ra khi gi instance method tng ng ca n. Lu rng proxy c sinh bi th vin CGLIB m hibernate s dng, chi tit tit v CGLIB c th tham kho http://cglib.sourceforge.net.
The generated SQL looks something like this: select event0_.id as id0_,event0_.name as name0_, event0_.location_id as location_id0_from events event0_ where event0_.id=? When you call loc.getName(), the following generated SQL is executed: select location0_.id as id0_as id0_,location0_.name as name0_ from locations location0_where location0_.id=?

Nhn xt : nu c id ca Location trong Event th ta hon ton c th ly v Location bt c lc no da vo ID ca n m khng nht thit ly ngay Location cng vi Event . bn cht ca proxy.

Collections : khi mt persistent class c cha mt Collection th ta cng c th xem xt n


nh mt lazily thng thng . V d trong Event class c mt tp hp/nhiu Speaker object l mt Collection nh sau
public class Event { private Set speakers; }

Khi ta s dng set element mapping.


<set name="speakers"> <key column="event_id"/> <one-to-many class="Speaker"/> </set>

Ch chiu quan h : one-to-many v many-to-one Trong - thuc tnh name ch ra property tng ng trong Event class,. - key element ch ra column lu gi foreign key ca table pha many tc y l table ca Speaker class , foreign key ny cha gi tr chnh l id ca Event class. - One-to-many element vi thuc tnh class ch ra Class pha many chnh l cc Class trong Collection.

You might also like