<!-- [if !mso]> <style> v\:* {behavior:url(#default#VML);} o\:* {behavior:url(#default#VML);} w\:* {behavior:url(#default#VML);} .shape {behavior:url(#default#VML);} </style> <![endif]--><!-- [if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:PunctuationKerning/> <w:DrawingGridVerticalSpacing>7.8 磅</w:DrawingGridVerticalSpacing> <w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery> <w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery> <w:ValidateAgainstSchemas/> <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid> <w:IgnoreMixedContent>false</w:IgnoreMixedContent> <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText> <w:Compatibility> <w:SpaceForUL/> <w:BalanceSingleByteDoubleByteWidth/> <w:DoNotLeaveBackslashAlone/> <w:ULTrailSpace/> <w:DoNotExpandShiftReturn/> <w:AdjustLineHeightInTable/> <w:BreakWrappedTables/> <w:SnapToGridInCell/> <w:WrapTextWithPunct/> <w:UseAsianBreakRules/> <w:DontGrowAutofit/> <w:UseFELayout/> </w:Compatibility> <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel> </w:WordDocument> </xml><![endif]--><!-- [if gte mso 9]><xml> <w:LatentStyles DefLockedState="false" LatentStyleCount="156"> </w:LatentStyles> </xml><![endif]--><!-- [if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable {mso-style-name:普通表格; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;} </style> <![endif]--><!-- [if gte mso 9]><xml> <o:shapedefaults v:ext="edit" spidmax="1030"/> </xml><![endif]--><!-- [if gte mso 9]><xml> <o:shapelayout v:ext="edit"> <o:idmap v:ext="edit" data="1"/> </o:shapelayout></xml><![endif]-->
继承映射
Animal 表
Id
|
name
|
Sex
|
Weight
|
Height
|
Type
|
1
|
小猪
|
True
|
100
|
|
P
|
2
|
小鸟
|
False
|
|
20
|
B
|
Extends.hbm.xml 如下:
< hibernate-mapping package = "com.test.hibernate" >
< class name = "Animal" table = "animal" >
< id name = "id" >
< generator class = "native" ></ generator >
</ id >
< property name = "name" ></ property >
< property name = "sex" ></ property >
< discriminator column = "type" type = "string" />
< subclass name = "Pig" discriminator-value = "P" >
< property name = "weight" ></ property >
</ subclass >
< subclass name = "Bird" discriminator-value = "B" >
< property name = "height" ></ property >
</ subclass >
</ class >
</ hibernate-mapping >
Animal animal =(Animal)session.load(animal.Class, 1);
if (animal instanceof Pig) {
System. out .println( "Pig" );
}
当用 Pig 去加载时是可以的,用父类 Animal 去加载也可以,但是 load 默认是 lazy 加载,用 Animal 加载的是 animal 代理类不是真正的 Pig 类
如果你把 class 中的 lazy=”false”, 则用 Animal 加载的是是真正的 Pig 类(多态)
用 get() 方法和 sql 查询支持多态查询,返回的是真正的 Pig 类
Sql 的“ from java.lang.Object ”是查到所有的实体类对象;
2 每个类一张映射表
animal 表
Id
|
Name
|
sex
|
1
|
小猪
|
True
|
2
|
小鸟
|
false
|
pig 表
bird 表
Extends.hbm.xml 如下:
< hibernate-mapping package = "com.test.hibernate" >
< class name = "Animal" table = "animal" >
< id name = "id" >
< generator class = "native" ></ generator >
</ id >
< property name = "name" ></ property >
< property name = "sex" ></ property >
<join- subclass name = "Pig" table=”pig” >
<key column=”pid” />
< property name = "weight" ></ property >
</ join- subclass >
< join- subclass name = "Bird" table=”bird” >
<key column=”bid” />
< property name = "height" ></ property >
</ join- subclass >
</ class >
这种对象模型不变而存储模型的方法变了;缺点是建表多,效率低;
如果 User 对象的一些属性也是其他对象的属性(即他们有许多共同的属性),因此将共同的属性提取成为一个公用的类(此类包含了共同的属性)如:
public class User {
private int id ;
private Contact conta ct ;
}
//set,get 方法略
public class Contact {
private String name ;
private boolean sex ;
}
//set,get 方法略
User.hbm.xml 如下:
< hibernate-mapping package = "com.test.hibernate" >
< class name = "User" table = "user" >
< id name = "id" >
< generator class = "native" ></ generator >
</ id >
<component name= "contact">
< property name = "name" ></ property >
< property name = "sex" ></ property >
</component>
</ class >
对关系模型没有任何影响,这样适合复用;
复合主键映射
通常将复合主键相关的属性放到一个单独的类中,这个是有约束的:
l 必须实现序列化接口
l 必须写 hashCode() 和 equals()
public class User {
private int age ;
private PK composite ;// 复合主键
}
//set,get 方法略
public class PK {
private String name ;
private boolean sex ;
}
//set,get 方法略
User.hbm.xml 如下:
< hibernate-mapping package = "com.test.hibernate" >
< class name = "User" table = "user" >
<composite-id name= " composite ">
<key- property name = "name" ></key- property >
<key- property name = "sex" ></key- property >
</composite-id>
< property name = "name" ></ property >
</ class >
集合的映射
t_collection_mapping 表
id
|
name
|
1
|
xxx
|
t_set_values 表
t_list_values 表
list_id
|
list_value
|
list_index
|
1
|
c
|
0
|
1
|
d
|
- 大小: 7.1 KB
分享到:
相关推荐
自动生成hibernate映射文件和实体类
关于hibernate映射和查询,使用MySQL,适合初学者的练习
hibernate映射枚举类型,hibernate如何映射枚举类型
build.xml 手写自动生成Hibernate映射文件和配置文件源代码
用来生成hibernate映射文件.从而实现不需手动写入。。。很实用的东东哦。。
常用 Hibernate 映射配置说明.doc
根据实体类生成简单的Hibernate映射文件,对不用Myeclipse的童鞋还是有用
myeclipse自动生成hibernate映射文件
Hibernate映射解析 七种映射关系
用Hibernate映射继承关系
Myeclipse生成Hibernate映射文件
hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,hibernate,包含4个说明文档,分别详细解说了hibernate关联映射的...
用MyEclipse自动生成hibernate映射文件和实体类
Hibernate映射配置实例大全:详细总结了Hibernate的各种映射配置的方法,并逐一举例
Hibernate映射集合属性List,用于映射集合属性 List Hibernate 自动生成表 不需sql
hibernate 映射关系 小结
hibernate 映射关系学习入门 多对多实体映射 源码
注:为节省空间,程序中需要的jar包,均在HibernateManytoManyMapCascadingSave.zip\HibernateManytoManyMapCascadingSave\lib\ <br>Hibernate 多对多实体映射实例 <br>学习Hibernate 实体映射的映射的好帮手...
Hibernate映射配置详解