`
wangtong40
  • 浏览: 248752 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Hibernate的继承策略

    博客分类:
  • ORM
阅读更多

<!-- [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 !mso]> <span class="mceItemObject" classid="clsid:38481807-CA0E-42D2-BF39-B33AF135CC4D" id=ieooui> </span> <style> st1\:*{behavior:url(#ieooui) } </style> <![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 !supportLists]-->节 1.01   <!-- [endif]-->每个带有联合的具体类一张表 – union-class

<!-- [if gte vml 1]><v:shapetype id="_x0000_t75" coordsize="21600,21600" o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe" filled="f" stroked="f"> <v:stroke joinstyle="miter"/> <v:formulas> <v:f eqn="if lineDrawn pixelLineWidth 0"/> <v:f eqn="sum @0 1 0"/> <v:f eqn="sum 0 0 @1"/> <v:f eqn="prod @2 1 2"/> <v:f eqn="prod @3 21600 pixelWidth"/> <v:f eqn="prod @3 21600 pixelHeight"/> <v:f eqn="sum @0 0 1"/> <v:f eqn="prod @6 1 2"/> <v:f eqn="prod @7 21600 pixelWidth"/> <v:f eqn="sum @8 21600 0"/> <v:f eqn="prod @7 21600 pixelHeight"/> <v:f eqn="sum @10 21600 0"/> </v:formulas> <v:path o:extrusionok="f" gradientshapeok="t" o:connecttype="rect"/> <o:lock v:ext="edit" aspectratio="t"/> </v:shapetype><v:shape id="_x0000_i1026" type="#_x0000_t75" style="width:401.25pt; height:129pt" mce_style="width:401.25pt; height:129pt"> <v:imagedata src="file:///C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\msohtml1\03\clip_image001.png" mce_src="file:///C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\msohtml1\03\clip_image001.png" o:title="3"/> </v:shape><![endif]--><!-- [if !vml]--><!-- [endif]-->

    <!-- 注:抽象的超类或者接口必须声明为 abstract ,否则超类实例需要一张单独表 -->

    < class name = "hibernate.hierarchical.BillingDetails" abstract = "true" >

< union-subclass name = "hibernate.hierarchical.CreditCard" table = "CREDIT_CARD" >

           < property name = "number" column = "NUMBER" />

           < property name = "expMonth" column = "EXP_MONTH" />

           < property name = "expYear" column = "EXP_YEAR" />

       </ union-subclass >

       < union-subclass name = "hibernate.hierarchical.BankAccount" table = "BANK_ACCOUNT" >

           < property name = "account" column = "ACCOUNT" />

           < property name = "bankname" column = "BANKNAME" />

           < property name = "swift" column = "SWIFT" />

       </ union-subclass >

    </ class >

public class BillingDetails { private Long id ; private String owner ;}

public class BankAccount extends BillingDetails {

private String account ; private String bankname ; private String swift ;}

public class CreditCard extends BillingDetails {

private String number ; private String expMonth ; private String expYear ;}

 

查询 BillingDetails 时生成的 SQL 如下:

Select BILLING_DETAIL_ID,OWNER… FROM

(

       Select BILLING_DETAIL_ID_OWNER,NUMBER,EXP_MONTH,EXMP_YEAR,null as ACCOUNT,null AS BANKNAME,null as SWIFT 1 as CLAZZ_

FROM CREDIT_CARD

       UNION

Select BILLING_DETAIL_ID_OWNER,null as NUMBER,null as EXP_MONTH,null as EXP_YEAR,ACCOUNT,BANKNAME,SWIFT,2 as CLAZZ_

       FROM BANK_ACCOUNT

)

这样使用的效率最高

<!-- [if !supportLists]-->节 1.02   <!-- [endif]-->每个类层次结构一张表 – SubClass

<!-- [if gte vml 1]><v:shape id="_x0000_i1027" type="#_x0000_t75" style="width:248.25pt;height:177.75pt" mce_style="width:248.25pt;height:177.75pt"> <v:imagedata src="file:///C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\msohtml1\03\clip_image003.png" mce_src="file:///C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\msohtml1\03\clip_image003.png" o:title="2"/> </v:shape><![endif]--><!-- [if !vml]--><!-- [endif]-->

注:

<!-- [if !supportLists]-->1、  <!-- [endif]-->在表设计时添加辨别标志 (BILLING_DETAILS_TYPE) 用于在持久化类之间区分不同对象。

<!-- [if !supportLists]-->2、  <!-- [endif]-->在列名称设计上需要加前缀(比如 CC/BA )等,用于让 Hibernate 自动识别不同对象。

 

    < class name = "hibernate.hierarchical.BillingDetails" table = "Billing_Details" >

       < property name = "owner" column = "OWNER" type = "string" />

       <!-- 添加一个辨别标志以便在持久化类之间进行区分 -->

       < discriminator column = "BILLING_DETAILS_TYPE" type = "string" />

       < subclass name = "hibernate.hierarchical.CreditCard" discriminator-value = "CC" >

           < property name = "number" column = "CC_NUMBER" />

           < property name = "expMonth" column = "CC_EXP_MONTH" />

           < property name = "expYear" column = "CC_EXP_YEAR" />

       </ subclass >

       < subclass name = "hibernate.hierarchical.BankAccount" discriminator-value = "BA" >

           < property name = "account" column = "BA_ACCOUNT" />

           < property name = "bankname" column = "BA_BANKNAME" />

           < property name = "swift" column = "BA_SWIFT" />

       </ subclass >

    </ class >

public class BillingDetails { private Long id ; private String owner ;}

public class BankAccount extends BillingDetails {

private String account ; private String bankname ; private String swift ;}

public class CreditCard extends BillingDetails {

private String number ; private String expMonth ; private String expYear ;}

 

当查询父表 BillingDetails 时将 BILLING_DETAILS 表字段全部查询。

Select BILLING_DETAILS_ID,CC_NUMBER,CC_EXP_MONTH… FROM BILLING_DETAILS

当查询子表 BankAccount/CreditCard 表时通过标识字段进行限制。

Select BILLING_DETAILS_ID,CC_NUMBER,CC_EXP_MONTH FROM BILLING_DETAILS WHERE BILLING_DETAILS_TYPE=’CC’

注:当类继承关系比较多时,存在性能问题

 

<!-- [if !supportLists]-->节 1.03   <!-- [endif]-->每个子类一个表 – JoinSubClass

将一个表示逻辑的大表拆分成多个子表,每个子表通过主键 / 外键的关系同父表进行关联,从而形成继承关系。每个子表的主键同时时父表的外键

<!-- [if gte vml 1]><v:shape id="_x0000_i1025" type="#_x0000_t75" style="width:414.75pt;height:258pt" mce_style="width:414.75pt;height:258pt"> <v:imagedata src="file:///C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\msohtml1\03\clip_image005.png" mce_src="file:///C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\msohtml1\03\clip_image005.png" o:title="1"/> </v:shape><![endif]--><!-- [if !vml]--><!-- [endif]-->

< class name = "hibernate.hierarchical.BillingDetails" table = "BillingDetails" >

< joined-subclass name = "hibernate.hierarchical.CreditCard" table = "CREDIT_CARD" >

<!-- 通过 key 指定的字段将父表和子表关联。 CREDIT_CARD_ID 即使子表的主键同时也是父表的外键 -->

           < key column = "CREDIT_CARD_ID" />

           < property name = "number" column = "NUMBER" />

           < property name = "expMonth" column = "EXP_MONTH" />

           < property name = "expYear" column = "EXP_YEAR" />

       </ joined-subclass >

       < joined-subclass name = "hibernate.hierarchical.BankAccount" table = "BANK_ACCOUNT" >

           < key column = "BANK_ACCOUNT_ID" />

           <

分享到:
评论

相关推荐

    Hibernate案例与专题-继承映射策略

    Hibernate案例与专题-继承映射策略。详细介绍了Hibernate继承映射策略。

    Hibernate继承映射的第一种策略:每棵类继承树对应一张表

    博文链接:https://shaqiang32.iteye.com/blog/201319

    Hibernate数据关联映射与继承策略

    Hibernate数据关联映射与继承策略.rar

    Hibernate实体对象继承的三种方法

     hibernate继承策略总共有三种,一种是共用一张表;一种是每个类一张表,表里面储存子类的信息和父类的信息;还有一种是通过表连接的方式,每个类都有一张表,但是子类对应的表只保存自己的信息,父类对应的表保存...

    Hibernate继承映射的第一种策略:每个具体类一张表

    博文链接:https://shaqiang32.iteye.com/blog/201321

    Hibernate继承映射的第一种策略:每个类对应一张表

    博文链接:https://shaqiang32.iteye.com/blog/201320

    彻底解决hibernate常见难点.zip

    Hibernate处理1-N关系时保存技巧、Hibernate缓存机制、Hibernate批量处理数据、Hibernate三种继承映射策略、hibernate映射体系、Hibernate主键生成策略、持久层DAO设计建议、基于xml文件的bean、使用HibernateAPI在...

    Hibernate学习笔记

    001 Hibernate 简介(开源 O/R 映射框架) 002 第一个 Hibernate 示例 003 hibernate 主要接口介绍 004 持久对象的生命周期介绍 005 query 接口初步 006 开源 O/R 映射框架内容回顾 ...029 hibernate 抓取策略

    Hibernate+中文文档

    3.8. Hibernate SQL方言 (hibernate.dialect) 3.9. Hibernate日志类别 3.10. JTA TransactionManagers 9.1. 继承映射特性(Features of inheritance mappings) 16.1. 别名注射(alias injection names) 19.1. ...

    hibernate3.2中文文档(chm格式)

    HIBERNATE - 符合Java习惯的关系数据库持久化 Hibernate参考文档 3.2 -------------------------------------------------------------------------------- 目录 前言 1. 翻译说明 2. 版权声明 1. Hibernate...

    HibernateAPI中文版.chm

    HIBERNATE - 符合Java习惯的关系数据库持久化 Hibernate参考文档 3.2 -------------------------------------------------------------------------------- 目录 前言 1. 翻译说明 2. 版权声明 1. Hibernate...

    hibernate学习笔记

    Hibernate 学习笔记 Hibernate 学习笔记 1 第一个hibernate项目(hibernate_first) 2 测试实体对象的生命周期(hibernate_session) 3 ...hibernate抓取策略 53 Hibernate最佳实践(Best Practices) 55

    精通 Hibernate:Java 对象持久化技术详解(第2版).part2

     5.1.2 Hibernate访问持久化类属性的策略  5.1.3 在持久化类的访问方法中加入程序逻辑  5.1.4 设置派生属性  5.1.5 控制insert和update语句  5.2 处理SQL引用标识符  5.3 创建命名策略  5.4 设置数据库Schema...

    Hibernate 课件及源代码

    内容涵盖安装 Hibernatetools 插件、Session 核心方法、持久化对象生命周期、对象关系映射(1-n、1-1、n-n、继承映射)、检索策略、检索方式(对象导航图、OID 检索、HQL、QBC、本地SQL)、Hibernate 一\二级缓存、...

Global site tag (gtag.js) - Google Analytics