NHibernate Step By Step(2)-继承映射
2009-08-02 16:24 Clingingboy 阅读(1668) 评论(2) 编辑 收藏 举报分三种
1.单表继承
既然是单表继承就需要一个键值来标识类别
- class以discriminator-value来标识类别
- discriminator 用以表示鉴别器的表字段
- 继承的类以subclass来标识
- 以extends来标识继承的类
(1)父类
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="false"> <class name="NHibernate.Test.Subclass.SubclassBase, NHibernate.Test" table="one_tab" discriminator-value="SUPER" > <id name="Id" type="Int32" unsaved-value="0" column="joined_subclass_id" access="nosetter.camelcase-underscore" > <generator class="native" /> </id> <discriminator column="disc_column" type="String" /> <property name="TestLong" column="test_long" type="Int64" /> <property name="TestString" column="test_string" type="String" /> <property name="TestDateTime" column="test_date_time" type="DateTime" /> </class> </hibernate-mapping>
(2)子类
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="false"> <subclass extends="NHibernate.Test.Subclass.SubclassBase, NHibernate.Test" name="NHibernate.Test.Subclass.SubclassOne, NHibernate.Test" discriminator-value="SUB" > <property name="OneTestLong" column="one_test_long" type="Int64" /> </subclass> </hibernate-mapping>
2.鉴别器
这里看一下鉴别器的使用,可以指定鉴别器的值,以辨别不同的类,如下:
多个类映射到同张表,Foo 类以Green标识,Baz 则具备了Color 属性,Foo可以说是Baz的特殊类别,都是通过配置文件来实现
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="false" namespace="NHibernate.Test.Subclass.EnumDiscriminator" assembly="NHibernate.Test"> <class name="Foo" table="subclass_enumdiscriminator" discriminator-value="Green"> <id name="Id" type="Int64"> <generator class="assigned"/> </id> <discriminator column="Color" type="NHibernate.Test.Subclass.EnumDiscriminator.Colors, NHibernate.Test"/> <subclass name="Bar" discriminator-value="Blue"/> </class> <class name="Baz" table="subclass_enumdiscriminator"> <id name="Id" type="Int64"> <generator class="assigned"/> </id> <property name="Color"/> </class> </hibernate-mapping>
测试数据
3.每个子类一张表(具备与父类有关联关系的)
映射文件
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="false"> <class name="NHibernate.Test.JoinedSubclass.Person, NHibernate.Test" table="person" proxy="NHibernate.Test.JoinedSubclass.Person, NHibernate.Test"> <id name="Id" type="Int32" unsaved-value="0" column="person_id" access="nosetter.camelcase-underscore"> <generator class="native"/> </id> <property name="Name" column="name"/> <property name="Sex" column="sex"/> <component name="Address"> <property name="Street"/> <property name="Zip"/> <property name="Country"/> </component> <joined-subclass name="NHibernate.Test.JoinedSubclass.Employee, NHibernate.Test" table="empl" proxy="NHibernate.Test.JoinedSubclass.Employee, NHibernate.Test"> <key column="person_id"/> <property name="Title" not-null="true" length="20"/> <property name="Salary" type="Decimal(19,5)"/> <many-to-one name="Manager"/> </joined-subclass> <joined-subclass name="NHibernate.Test.JoinedSubclass.Customer, NHibernate.Test" table="cust" proxy="NHibernate.Test.JoinedSubclass.Customer, NHibernate.Test"> <key column="person_id"/> <property name="Comments"/> <many-to-one name="Salesperson"/> </joined-subclass> </class> </hibernate-mapping>
4.无关联关系的继承表
即每张表都会生成父类的字段
<class name="Person" table="UPerson"> <id name="id" column="person_id" unsaved-value="0"> <generator class="hilo"/> </id> <property name="name" unique-key="NameUniqueKey" not-null="true" length="80"/> <property name="sex" not-null="true" update="false"/> <component name="address"> <property name="address" index="AddressIndex"/> <property name="zip" index="AddressIndex"/> <property name="country"/> </component> <union-subclass name="Employee" table="UEmployee"> <property name="title" not-null="true" length="20"/> <property name="salary" /> <many-to-one name="manager"/> </union-subclass> <union-subclass name="Customer" table="UCustomer"> <property name="comments"/> <many-to-one name="salesperson"/> </union-subclass> </class>
数据库表结构