[转]hibernate-xml配置模板
hibernate-xml配置模板
【Group.hbm.xml文件如下:】
<hibernate-mapping>
<classname="com.dragon.hibernate.Group"table="t_group">
<idname="id">
<generatorclass="native"/>
</id>
<propertyname="name"/>
</class>
</hibernate-mapping>
【User.hbm.xml文件如下:】
<hibernate-mapping>
<classname="com.dragon.hibernate.User"table="t_user">
<idname="id">
<generatorclass="native"/>
</id>
<propertyname="name"/>
<many-to-onename="group"column="groupid"/>
</class>
</hibernate-mapping>
---------------------------------------------------------
2.多对多双向映射(User<-Role)
【User.hbm.xml文件如下】
<hibernate-mapping>
<classname="com.dragon.hibernate.User"table="t_user">
<idname="id">
<generatorclass="native"/>
</id>
<propertyname="name"/>
<setname="roles"table="t_user_role">
<keycolumn="userid"/>
<many-to-manyclass="com.dragon.hibernate.Role"column="roleid"/>
</set>
</class>
</hibernate-mapping>
【Role.hbm.xml文件如下】
<hibernate-mapping>
<classname="com.dragon.hibernate.Role"table="t_role">
<idname="id">
<generatorclass="native"/>
</id>
<propertyname="name"/>
<setname="users"table="t_user_role">
<keycolumn="roleid"/>
<many-to-manyclass="com.dragon.hibernate.User"column="userid"/>
</set>
</class>
</hibernate-mapping>
---------------------------------------------------------
3.多对多单向映射(User<-Role)
在User端使用set引用role维持关系,则User.hbm.xml文件同上面的双向一样,只有Role.hbm.xml文件稍微有所小小的不同,比双向更简单。
【Role.hbm.xml文件如下】
<hibernate-mapping>
<classname="com.dragon.hibernate.Role"table="t_role">
<idname="id">
<generatorclass="native"/>
</id>
<propertyname="name"/>
</class>
</hibernate-mapping>
【User.hbm.xml文件如下】
<hibernate-mapping>
<classname="com.dragon.hibernate.User"table="t_user">
<idname="id">
<generatorclass="native"/>
</id>
<propertyname="name"/>
<setname="roles"table="t_user_role">
<keycolumn="userid"/>
<many-to-manyclass="com.dragon.hibernate.Role"column="roleid"/>
</set>
</class>
</hibernate-mapping>
---------------------------------------------------------
4.一对一主键双向(Person?->IdCard)
【Person.hbm.xml文件如下】
<hibernate-mapping>
<classname="com.dragon.hibernate.Person"table="t_person">
<idname="id">
<generatorclass="foreign">
<paramname="property">idcard</param>
</generator>
</id>
<propertyname="name"/>
<one-to-onename="idcard"constrained="true"/>
</class>
</hibernate-mapping>
【Idcard.hbm.xml文件如下】
<hibernate-mapping>
<classname="com.dragon.hibernate.IdCard"table="t_idcard">
<idname="id">
<generatorclass="native"/>
</id>
<propertyname="idno"/>
<one-to-onename="person"></one-to-one>
</class>
</hibernate-mapping>
One-to-one标签指示hibernate如何加载关联对象,默认是根据主键加载;
constrained="true"表示当前主键存在一个约束,person的主键作为外键参照idcard。
---------------------------------------------------------
5.一对一主键单向(Person-->IdCard)
Person.hbm.xml文件如下相同
Idcard.hbm.xml文件如下:
<hibernate-mapping>
<classname="com.dragon.hibernate.IdCard"table="t_idcard">
<idname="id">
<generatorclass="native"/>
</id>
<propertyname="idno"/>
</class>
</hibernate-mapping>
与双向的只是少了这句<one-to-onename="person"></one-to-one>
6.一对一外键单向(Person-->IdCard)
IdCard.hbm.xml文件如下:
<hibernate-mapping>
<classname="com.dragon.hibernate.IdCard"table="t_idcard">
<idname="id">
<generatorclass="native"/>
</id>
<propertyname="idno"/>
</class>
</hibernate-mapping>
Person.hbm.xml文件如下:
<hibernate-mapping>
<classname="com.dragon.hibernate.Person"table="t_person">
<idname="id">
<generatorclass="native"/>
</id>
<propertyname="name"/>
<many-to-onename="idcard"column="idcardid"unique="true"/>
</class>
</hibernate-mapping>
---------------------------------------------------------
7.一对一外键双向(Person<-->IdCard)
Person.hbm.xml文件与单向一样
IdCard.hbm.xml文件如下:
<hibernate-mapping>
<classname="com.dragon.hibernate.IdCard"table="t_idcard">
<idname="id">
<generatorclass="native"/>
</id>
<propertyname="name"/>
<one-to-onename="person"proper-ref=“idcard”/>
</class>
</hibernate-mapping>
---------------------------------------------------------
8.一对多单向(Group-->User)
注意,在key中的外键<keycolumn="groupid"/>要一致不然要生成中间表
【Group.hbm.xml文件如下】
<hibernate-mappingpackage="com.dragon.hibernate">
<classname="Group"table="t_group">
<idname="id">
<generatorclass="native"/>
</id>
<propertyname="name"/>
<setname="users">
<keycolumn="groupid"/>
<one-to-manyclass="User"/>
</set>
</class>
</hibernate-mapping>
【User.hbm.xml文件如下】
<hibernate-mapping>
<classname="com.dragon.hibernate.User"table="t_user">
<idname="id">
<generatorclass="native"/>
</id>
<propertyname="name"/>
</class>
</hibernate-mapping>
---------------------------------------------------------
9.一对多双向(Group<-->User)
【Group.hbm.xml文件如下】
<hibernate-mappingpackage="com.dragon.hibernate">
<classname="Group"table="t_group">
<idname="id">
<generatorclass="native"/>
</id>
<propertyname="name"/>
<setname="users"cascade="all">
<keycolumn="groupid"/>
<one-to-manyclass="User"/>
</set>
</class>
</hibernate-mapping>
【User.hbm.xml文件如下:】
<hibernate-mapping>
<classname="com.dragon.hibernate.User"table="t_user">
<idname="id">
<generatorclass="native"/>
</id>
<propertyname="name"/>
<many-to-onename="groups"column="groupid"/>
</class>
</hibernate-mapping>
---------------------------------------------------------
10.继承映射(Pig和Bird继承Animal)
--一棵继承树映射成一张表
extends.hbm.xml文件如下:
<hibernate-mappingpackage="com.dragon.hibernate">
<classname="Animal"table="t_extends">
<idname="id">
<generatorclass="native"/>
</id>
<discriminatorcolumn="type"type="string"/>
<propertyname="name"/>
<propertyname="sex"/>
<subclassname="Pig"discriminator-value="P">
<propertyname="weight"/>
</subclass>
<subclassname="Bird"discriminator-value="B">
<propertyname="height"/>
</subclass>
</class>
</hibernate-mapping>
---------------------------------------------------------
11.继承映射(Pig和Bird继承Animal)--每个具体类映射成一张表joined-classs
extends.hbm.xml文件如下:
<hibernate-mappingpackage="com.dragon.hibernate">
<classname="Animal"table="t_animal">
<idname="id">
<generatorclass="native"/>
</id>
<propertyname="name"/>
<propertyname="sex"/>
<joined-subclassname="Pig"table="t_pig">
<keycolumn="pid"/>
<propertyname="weight"></property>
</joined-subclass>
<joined-subclassname="Bird"table="t_bird">
<keycolumn="bid"/>
<propertyname="height"></property>
</joined-subclass>
</class>
</hibernate-mapping>
---------------------------------------------------------
12.继承映射(Pig和Bird继承Animal)--每个子类类映射成一张表union-subclass
extends.hbm.xml文件如下:
<hibernate-mappingpackage="com.dragon.hibernate">
<classname="Animal"table="t_animal">
<idname="id">
<generatorclass="uuid"/>
</id>
<propertyname="name"/>
<propertyname="sex"/>
<union-subclassname="Pig"table="t_pig">
<propertyname="weight"/>
</union-subclass>
<union-subclassname="Bird"table="t_bird">
<propertyname="height"/>
</union-subclass>
</class>
</hibernate-mapping>
**注释在使用<idname="id">
<generatorclass="uuid"/>
</id>是不能将<generatorclass="uuid"/>改写成<generatorclass="native"/>,不然会出错。相应的Animal类中的id字段类型也要该为String.
如果在<classname="Animal"table="t_animal">中添加Abstract="true"可以使hibernate不生成表t_animal
---------------------------------------------------------
13.component映射
component.hbm.xml文件如下:
<hibernate-mapping>
<classname="com.bjsxt.hibernate.User"table="t_user">
<idname="id">
<generatorclass="native"/>
</id>
<propertyname="name"/>
<componentname="contact">
<propertyname="email"/>
<propertyname="address"/>
<propertyname="zipCode"/>
<propertyname="contactTel"/>
</component>
</class>
</hibernate-mapping>
User类中有以下属性:
privateintid;
privateStringname;
privateContactcontact;它不再是一种引用了
14.Set,Map,Arrary[],List映射
---------------------------------------------------------
CollectionMapping.hbm.xml文件如下:
<hibernate-mapping>
<classname="com.bjsxt.hibernate.CollectionMapping"table="t_CollectionMapping">
<idname="id">
<generatorclass="native"/>
</id>
<propertyname="name"/>
<setname="setValue"table="t_set_value">
<keycolumn="set_id"/>
<elementtype="string"column="set_value"/>
</set>
<listname="listValue"table="t_list_value">
<keycolumn="list_id"/>
<list-indexcolumn="list_index"/>
<elementtype="string"column="list_value"/>
</list>
<arrayname="arrayValue"table="t_array_value">
<keycolumn="array_id"/>
<list-indexcolumn="array_index"/>
<elementtype="string"column="array_value"/>
</array>
<mapname="mapValue"table="t_map_value">
<keycolumn="map_id"/>
<map-keytype="string"column="map_key"/>
<elementtype="string"column="map_value"/>
</map>
</class>
</hibernate-mapping>