Professional Documents
Culture Documents
Creating Mapping Definitions
Creating Mapping Definitions
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
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.
Many-to-one element:
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 :
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.
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.