Hibernate中的继承关系的映射实现
- 格式:pdf
- 大小:911.53 KB
- 文档页数:3
Hibernate simplifies inheritance mappingHibernate is an object-relational mapping and persistence framework that provides a lot of advanced features, ranging from introspection to polymorphism and inheritance mapping. But mapping class hierarchies to a relational database model might prove somewhat difficult. This article covers three strategies that you can use in your everyday programming to easily map complex object models to relational database models. OverviewHibernate is a pure Java object-relational mapping and persistence framework that allows you to map plain old Java objects to relational database tables using XML configuration files. Using Hibernate can save a lot of development time on a project, since the whole JDBC layer is managed by the framework. This means that your application's data access layer will sit above Hibernate and be completely abstracted away from of the underlying data model.Hibernate has a number of advantages over other similar object-relational mapping approaches (JDO, entity beans, in-house development, and so on): it's free and open source, it has achieved a good level of maturity, it's widely used, and it has a very active community forum.To integrate Hibernate into an existing Java project, you'll need to walk through the following steps:1.Download the latest release of the Hibernate framework from the Hibernate Website. Copy the necessary Hibernate libraries (JAR files) to your application'sCLASSPATH.2.Create the XML configuration files that will be used to map your Java objects toyour database tables. (We'll describe that process in this article.)3.Copy the XML configuration files to your application's CLASSPATH.You'll notice that you don't have to modify any Java objects to support the framework. Imagine, for instance, that you needed to somehow change a database table that your Javaapplication used -- by renaming a column, for example. Once you'd changed the table, all you'd have to do to update your Java application would be to update the appropriate XML configuration file. You wouldn't need to recompile any Java code.Hibernate Query Language (HQL)Hibernate provides a query language called Hibernate Query Language (HQL), which is quite similar SQL. For those of you who prefer good old-fashioned SQL queries, Hibernate still gives you the opportunity to use them. But our supporting example will use HQL exclusively.HQL is quite simple to use. You will find all the familiar keywords you know from SQL, like SELECT, FROM, and WHERE. HQL differs from SQL in that you don't write queries directly on your data model (that is, on your tables, columns, etc.), but rather on you Java objects, using their properties and relationships.Listing 1 illustrates a basic example. This HQL code retrieves all Individuals whose firstName is "John."You can refer to the HQL reference material on the Hibernate Website if you want to learn more about HQL syntax.XML configuration filesThe core of Hibernate's functionality resides inside XML configuration files. These files must reside in your application's CLASSPATH. We placed them in the config directory of our sample code packageThe first file that we'll examine is hibernate.cfg.xml. It contains information regarding your data source (DB URL, schema name, username, password, etc.) and references to other configuration files that will contain your mapping information.The remaining XML files allow you to map Java classes against database tables. We will take a closer look at those files later, but it is important to know that their filenames follow the pattern ClassName.hbm.xml.Our supporting exampleIn this article, we'll examine a basic example that illustrates how Hibernate works and puts to good use three different strategies under which you can use Hibernate to do your object-relational mapping. Our example application will be used by an insurance company that must keep legal records of all the property rights that its customers are insured for. We've provided the full source code with this article; this code provides basic functionality from which you could build a full-fledged application, such as a Web or Swing application.Our example assumes a classic use case for this kind of application. The user would provide the search criteria for any type of customer (individual, corporation, government agency, etc.), and would then be presented with a list of all customers matching the specified criteria -- even if these are different types of customers. The user could access a more detailed view of a specific customer from that same list.In our application, a property right is represented by the Right class. A Right can either be a Lease or a Property. A Right is owned by a customer. To represent our customer, we'll use the generic class Person. A Person can either be an Individual or a Corporation. Of course, the insurance company must know the Estates to which those Rights are assigned. An Estate is a very generic term, you'll agree. So, we'll use the Land and Building classes to give our developers more comprehensive objects to work with.From this abstract, we can develop the class model shown in Figure 1:Figure 1. Complete Class ModelOur database model was designed to cover the three different strategies we'll discuss in this article. For the Right hierarchy, we'll use a single table (TB_RIGHT) and map to the correct class using the DISCRIMINATOR column. For the Person hierarchy, we'll use what we call a super table(TB_PERSON) that will share the same IDs with two othertables (TB_CORPORATION and TB_INDIVIDUAL). The third hierarchy (Estate) usestwo different tables (TB_BUILDING and TB_LAND) linked by a foreign key defined bythe combination of two columns (REF_ESTATE_ID and REF_ESTATE_TYPE).Figure 2. Complete data modelSetting up the databaseHibernate supports a wide variety of RDBMSs, and any of them should work with our sample. However, the sample code and the text of this article have been tailored for HSQLDB, a fully functional relational database written entirely in the Java language. In the sql directory of the sample code package, you will find a file named datamodel.sql. This SQL script will create the data model used in our example.Setting up the Java projectAlthough you can always build and execute the sample code using the command line, you may want to consider setting up the project in an IDE for better integration. Within the sample code package, you'll find the following directories:∙config, which contains all the sample's XML configuration files (mapping, Log4J, etc.)∙data, which contains configuration files used by HSQLDB. You will also find a batch file named startHSQLDB.bat that you may use to launch the database.∙src, contains all the sample's source code.Be sure to copy the required Java libraries and XML configuration files to you application's CLASSPATH. The code needs only the Hibernate and HSQLDB libraries in order to compile and run properly.Strategy 1: One table per subclass (Persons)In our first strategy, we'll look at how to map our Person hierarchy. You'll notice that the data model is very close to our class model. As a result, we'll use a different table for each class in the hierarchy, but all these tables must share the same primary key (we'll explain that in more detail momentarily). Hibernate will then use this primary key when it inserts new records into the database. It will also make use of this same primary key to perform JOIN operations when accessing the database.Now we need to map our object hierarchy to our table model. We have three tables (TB_PERSON, TB_INDIVIDUAL, and TB_CORPORATION). As we mentioned above, they all have a column named ID as a primary key. It's not mandatory to have a shared column name like this, but it is considered good practice -- and it makes it a lot easier to read the generated SQL queries.In the XML mapping file, shown in Listing 2, you'll notice that the two concrete classes are declared as <joined-subclass> inside the Person mapping definition. The XML element <id> is mapped to the primary key for the top-level table TB_PERSON, while the <key> elements (from each subclass) are mapped to the matching primary keys of theTB_INDIVIDUAL and TB_CORPORATION tables.Listing 2. Person.hbm.xml<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD2.0//EN""/hibernate-mapping-2.0.dtd"><hibernate-mapping><class name="eg.hibernate.mapping.dataobject.Person" table="TB_PERSON" polymorphism="implicit"><id name="id" column="ID"><generator class="assigned"/></id><set name="rights" lazy="false"><key column="REF_PERSON_ID"/><one-to-many class="eg.hibernate.mapping.dataobject.Right" /></set><joined-subclass name="eg.hibernate.mapping.dataobject.Individual"table="TB_INDIVIDUAL"><key column="id"/><property name="firstName" column="FIRST_NAME" type="ng.String" /> <property name="lastName" column="LAST_NAME" type="ng.String" /> </joined-subclass><joined-subclass name="eg.hibernate.mapping.dataobject.Corporation"table="TB_CORPORATION"><key column="id"/><property name="name" column="NAME" type="string" /><property name="registrationNumber" column="REGISTRATION_NUMBER" type="string" /></joined-subclass></class></hibernate-mapping>Saving a new instance of Individual form our Java code with Hibernate is pretty straightforward, as shown in Listing 3:In turn, Hibernate generates the two SQL INSERT requests illustrated in Listing 4. That's two requests for just one save().To access an Individual from the database, simply specify the class name in your HQL query, as shown in Listing 5.Listing 5. Invoke an HQL queryHibernate will then automatically perform the SQL JOIN to retrieve all the necessary information from both tables, as shown in Listing 6:However, when no concrete class is specified, Hibernate needs to perform an SQL JOIN, since it doesn't know which table to go through. Amongst all the retrieved table columns returned from the HQL query, an extra dynamic column will be returned as well. The clazz column is used by Hibernate to instantiate and populate the returned object. We call this class determination dynamic, as opposed to the method that we'll use in our second strategy.Listing 7 shows how to query an abstract Person given its id property, while Listing 8 shows the SQL query automatically generated by Hibernate, including the table junctions:Strategy 2: One Table per class herarchy (Rights)For our Right hierarchy, we'll use a single table (TB_RIGHT) to store the entire class hierarchy. You'll notice that the TB_RIGHT table possesses all the columns necessary to store every attribute of the Right class hierarchy. Values of the saved instances will then be saved in the table, leaving every unused column filled with NULL values. (Because it's full of "holes," we often call it a Swiss cheese table.)In Figure 3, you will notice that the TB_RIGHT table contains an additional column, DISCRIMINATOR. Hibernate uses this column to automatically instantiate the appropriate class and populate it accordingly. This column is mapped using the<discriminator> XML element from our mapping files.ConclusionIn this article, we tried to give you a fairly simple implementation example for three mapping strategies Hibernate provides. In retrospect, each strategy has its advantages and drawbacks:∙For our first strategy (one table per subclass), Hibernate will read multiple tables each time an object is instantiated and populated. That operation can produce good results if your indexes are well defined and your hierarchy isn't too deep. If that isnot the case, however, you may experience problems with overall performance.∙For our second strategy (one table per class hierarchy), you'll have to define your integrity using check constraints. This strategy might become difficult to maintain as the number of columns increases over time. On the other hand, you may choose not to use such constraints at all and rely on your application's code to manage itsown data integrity.Our third strategy (one table per concrete class) has some mapping limitations, and the underlying data model can't use referential integrity, meaning that you're notusing the relational database engine to its full potential. On the plus side, though,this strategy can be combined quite easily with the other two.Regardless of the strategy you choose, always keep in mind that you don't need to alter your Java classes in the process, meaning that there is absolutely no link between your business objects and the persistence framework. It's this degree of flexibility that is making Hibernate so popular in object-relational Java projects.Hibernate 简化继承映射Hibernate 是一个对象关系映射和持久性框架,它提供了许多高级特性,从内省到多态和继承映射。
一. H ibern ate工作使用步骤?1.读取并解析配置文件2. 读取并解析映射信息,创建Ses sionF actor y3.打开Sess sion4. 创建事务Tran satio n5.持久化操作6. 提交事务7.关闭Sess ion 8. 关闭Se sstio nFact ory 二.Hib ernat e的查询方式有几种?(1)导航对象图检索方式。
根据已经加载的对象,导航到其他对象。
(2)OID查询方式。
根据对象的O ID来查询对象。
Se ssion的get()和loa d()方法。
(3)HQL查询方式。
HQ L是面向对象的查询语言,ses sion的find()方法用于执行HQL查询语句。
可以利用Q uery接口。
Q ueryquery = se ssion.crea teQue ry(“f rom C ustom er as c wh ere c.name=: c ustom erNam e”);quer y.set Strin g(“cu stome rName”,”张三”);Listresul tList = qu ery.l ist();(4)QBC查询方式。
这种API封装了基于字符串形式的查询语句。
Crit eriacrite ria = sess ion.c reate Crite ria(U ser.c lass);Cr iteri on cr iteri on1 = Expe ssion.like(“nam e”,”T%”);Crite rioncrite rion2 = Ex pessi on.eq(age,new I ntege r(30));cr iteri a =crite ria.a dd(cr iteri on1);crit eria = cr iteri a.add(crit erion2);L ist r esult List= cri teria.list();这种查询方式使用的较少,主要是在查询中需要用户输入一系列的查询条件,如果采用HQL查询代码会比较烦。
1、ORM 有什么好处?AA).能进行关系对象的映射B).具有控制器功能C).能在服务器端保存客户端状态D).向DTO传递数据2、在使用property标签时,如果要显示标签的代码,需要设置下面哪个属性的属性值? CA).defaultB).valueC).escapeD).id3、以下哪一种检索策略利用了外连结查询?CA).立即检索B).延迟检索C).迫切左外连结检索D).迫切右外连结检索4、如果你不能确定你要寻找的对象的持久化标识符,那么你需要使用查询,使用Session 的什么方法? AA).createQuery()B).query()C).queryAll()D).queryObject()5、HQL查询语句from Cat as cat,不仅仅返回Cat的实例,而且还返回____的实例。
BA).Cat 父类B).Cat 子类C).Cat 实现接口D).Cat 兄弟类6、下面哪些子句不支持算数表达式?CA).compute by 和order byB).compute by 和insert byC).order by 和group byD).insert by 和group by7、使用Hibernate 的QBC 查询,要使用SQL 中的类似select count(*) from tablename 方法求出记录数,必须使用哪一个类?BA).RestrictionsB).ProjectionsC).CriteriaD).Criteron8、Struts2中默认的主题是哪一种?BA).simpleB).xhtmlC).css_xhtmlD).ajax9、在Hibernate 中,关于数据库的方言,下面哪个说法是正确的?DA).可以不写数据库方言B).数据库方言没有作用,可以随便写C).有通用的数据库方言可以使用D).数据方言对应于每个数据库,主要用于产生SQL 语句数据方言对应于每个数据库10、在JEE企业级开发中,SSH框架中Struts主要是完成Web的MVC中的哪个角色?AA).C(控制器)和V(视图)B).M (模型)和C(控制器)C).只有C(控制器)D).V(视图)和M(模型)11、在Struts2的包配置中,下面哪个属性是必须指定的?CA).namespaceB).extendsC).nameD).abstract12、通过实现下面哪个接口,可以很容易地实现一个拦截器类的编写?BA).ActionB).InterceptorC).AbstractInterceptorD).MethodInterceptor13、Action运行期间所有用到的数据都保存在下面哪个对象中?DA).RequestB).SessionC).ResponseD).ActionContext14、在使用SSH进行JEE企业级开发中,通常把模板文件放置在/WEB-INF目录下的原因是?AA).因为模板文件不能直接访问,为了防止用户直接访问模板文件,所以放在/WEB-INF目录下,让这些文件直接访问不到。
如何描述项目?1.讲需求1)电子商务的基本发展b2c.c2c.b2bB2B指的是Business to Business,即商家(泛指企业)对商家的电子商务。
电子商务的发展过程中还有C2C、B2C、C2B等模式。
B2B是指进行电子商务交易的供需双方都是商家(或企业、公司),她们使用了Internet的技术或各种商务网络平台,完成商务交易的过程。
这些过程包括:发布供求信息,订货及确认订货,支付过程及票据的签发、传送和接收,确定配送方案并监控配送过程等。
C2C即Consumer To Consumer。
C2C同B2B、B2C一样,都是电子商务的几种构成成份之一。
不同的是C2C是用户对用户的模式,C2C商务平台就是通过为买卖双方提供一个在线交易平台,使卖方可以主动提供商品上网拍卖,而买方可以自行选择商品进行竞价。
B2C是电子商务按交易对象分类中的一种,即表示商业机构对消费者的电子商务。
这种形式的电子商务一般以网络零售业为主,主要借助于Internet开展在线销售活动。
例如经营各种书籍、鲜花、计算机、通信用品等商品。
2)基本需求A.功能性需求门户,后台管理,业务流程B.其他POST.GET不安全的安全: SSL,非对称加密,公钥,私钥等等RSA性能:2.讲架构1)应用层次结构经典的基于MVC的JSP Model 2架构方式C 控制层,应用了Struts2核心控制组件V 表现层,服务器端:JSP和Struts标记库客户端:根据特定的需要应用了Ajax的异步方式M 业务层,没有分离出持久层,应用了Hibernate作为数据库的访问为什么要用Struts2,Hibernate,Ajax使用的Struts2好处能更简单的开发网站,用Struts2可以不用封装表单中的数据!Struts2会自动帮我们封装好,而Servlet就不那么好了,必须自己封装!同时Struts2结合hibernate那就相当简单了!往数据库传form中的数据不用封装,从数据库取也不用封装,省去了大量的垃圾代码,大大的提高了编码的效率!页面方面的话struts2有许多的标签可以用,在某些方面可以简化一些jsp代码。
SSH复习题选择题1、下面哪一个不是框架(D)。
A.SpringB.StrutC.HibernateD.JSP2、下面是框架的是(D)。
A.定义实体类B.数据的增删改查操作C.业务逻辑的描述D.页面展示和控制转发4、在三层结构中,Hibernate承担的任务是(A)。
A.数据的持久化操作B.实体类的定义C.业务逻辑的描述D.页面的显示与控制转发5、下面信息不在Strut2配置文件中配置的是(B)。
A.FormBean配置信息B.Spring声明式事务C.Action转发路径D.Strut2引用的资源文件6、在trut实现的框架中,(B)类包含了e某cute方法的控制器类,负责调用模型的方法,控制应用程序的流程。
A.Aja某B.ActionC.FormD.Method7、下面关于Hibernate的说法,错误的是(C)。
A.Hibernate是一个“对象-关系映射”的实现B.Hibernate是一种数据持久化技术C.Hibernate是JDBC的替代技术D.使用Hibernate可以简化持久化层的编码8、下列说法中错误的是(C)。
A.使用通配符可以优化action的配置B.约定优于配置。
约定的如果好,可以使action配置非常的简洁C.如果Action中存在多个方法时,只能使用method属性指定调用方法D.在trut2中超级链接通常都采用绝对路径,而不使用相对路径,这样便于链接的实现9、下列说法中错误的是(D)。
A.从值栈中取值用value=“参数名”,参数名是action中的参数B.从actionconte某t中取值用#parameter.参数名,参数名是trut中的参数C.在客户端跳转时一次requet只有一个valueStackD.在客户端跳转时一次requet可以有多个valueStack10、和SQL相比,HQL有哪些优点(C)。
A.HQL能够简单的操作表B.HQL是简单的ql语言组成C.HQL是面向对象的检索语言D.HQL对应的是表和表的字段11、一个某ML文件能被浏览器解析的最小要求是(A)。
SCME-G2阶段模拟考试三一、选择题(每题2分,共100分)1.数据库设计的最终目标,不包括()。
【选两项】A.高效 B.满足范式 C.安全 D.表现设计者实力2.假定有一个用户表,表中包含字段:userid (int)、username (varchar)、password(varchar)等,该表需要设置主键,以下说法正确的是()。
【选两项】A.如果不能有同时重复的username和password,那么username和password可以组合在一起作为主键。
B.此表设计主键时,根据选择主键的最小性原则,最好采用userid作为主键。
C.此表设计主键时,根据选择主键的最小性原则,最好采用username和password 作为组合键。
D.如果采用userid作为主键,那么在userid列输入的数值,允许为空。
3.关于子查询,以下说法正确的是()。
【选两项】A.一般来说,表连接都可以用子查询替换。
B.一般来说,子查询都可以用表连接替换。
C.相对于表连接,子查询适合于作为查询的筛选条件。
D.相对于表连接,子查询适合于查看多表的数据。
4.现有订单表orders,包含数据如下表。
若查询既订购了产品p01,又订购了产品p02的顾客编号,可以执行以下()sql语句。
【选两项】A.select distinct (cid) from orders ol where ol.pid in ('p01','p02')B.select distinct (cid) from orders ol where ol.pid = 'p01' and ol.pid = 'p02' C.select distinct (o1.cid) from orders o1,orders o2 where o1.pid='p01' and o2.pid='p02' and o2.cid = o1.cidD.select distinct(cid) from orders where pid = 'p01' and cid in(select cid from orders where pid='p02')5.项目开发需要经过几个阶段,绘制数据库的E-R图应该在()阶段进行。
Hibernate中的继承关系的映射实现
1、继承关系的描述
在域模型中,类与类之间除了关联关系和聚集关系,还可以存在继承关系,在下面的图中所示的域模型中,Company类和Employee类之间为一对多的双向关联关系(假定不允许雇员同时在多个公司兼职),Employee类为抽象类,因此它不能被实例化,它有两个具体的子类:HourlyEmployee类和SalariedEmployee 类。
由于Java只允许一个类最多有一个直接的父类,因此Employee类、HourlyEmployee类和SalariedEmployee类构成了一棵继承关系树。
2、OOP中的多态的概念---OOP中的多态
(1)OOP中的多态
在面向对象的范畴中,还存在多态的概念,多态建立在继承关系的基础上。
简单地理解,多态是指当一个Java应用变量被声明为基类的类型时(如定义为Employee类时),这个变量实际上既可以引用其各个子类的实例(如HourlyEmployee类的实例,也可以引用SalariedEmployee类的实例)。
(2)代码示例
以下这段程序代码就体现了多态:
List employees=businessService.findAllEmployees();
Iterator it=employees.iterator();
while(it.hasNext())
{
Employee oneEmployee=(Employee)it.next();
if(oneEmployee instanceof HourlyEmployee)
{
System.out.println(oneEmployee.getName()+""+((HourlyEmployee)oneEmployee).getRate());
}
else
{
System.out.println(oneEmployee.getName()+""+((SalariedEmployee)oneEmployee).getSalary());
}
3、多态查询的应用
(1)多态查询
如果我们在BusinessService类的findAllEmployees()方法中通过Hibernate API从数据库中检索出所有Employee对象(基类),则将会在findAllEmployees()方法返回的集合中将包含Employee基类的各个子类的对象,也就是既包含有HourlyEmployee类的实例,同时也包含SalariedEmployee类的实例,这种查询被称为多态查询。
以上程序中变量oneEmployee被声明为Employee类型,它实际上既可能引用HourlyEmployee类的实例,也可能引用SalariedEmployee类的实例。
(2)多态关联
此外,从Company类到Employee类为多态关联,因为Company类的employees集合中可以包含HourlyEmployee类和SalariedEmployee类的实例。
从Employee
类到Company类不是多态关联,因为Employee类的company
属性只会引用Company类本身的实例。
数据库表之间并不存在继承关系,那么如何把域模型的继承
关系映射到关系数据模型中呢
4、实现继承关系的三种映射方式
在Hibernate中对于继承映射到数据表有几种不同的策略,
各有适用的不同场合。
下面以Person基类以及其两个子类
Student和Professor类为例来说明。
(1)每个具体子类映射成单个数据库表----而抽象基类不参与映射------每个具体类一张表Person由于是抽象类,未
映射成数据库表;而Student、
Professor类映射为相应的表,
它们具有各自的主键。
●优点
报表操作实现
简单:表中包含了具体子类的所有信息。
●缺点
类的修改会导致相对应的表及其子类所对应表的更改
角色的更改会造成ID的重新赋值(因为不同子类的ID可能重复)
难以在支持多重角色时,保持数据的完整性。
这种策略适合在类层次结构上有一定数量的抽象类的情况下使用,同样有两种方式,一种是采用显式多态的方式,另一种是采用隐式多态的方式,显式多态采用的为@hibernate.union-subclass的方式,隐式多态则采用每个具体类的PO独立建表的策略,在它的映射文件中将看不出任何的和接口、抽象类的关系,同时对于抽象类,需要指明其abstract=”true”。
关系数据模型完全不支持域模型中的继承关系和多态。
(2)将整个类层次映射为单个数据库表-----单表策略
单表策略很容易理解,就是将基类、各个子类中所有的属性都放
至一张表中,这对于子类属性不多的情况非常有效----每个子类代表
表中由识别器列(discriminator column)标识的一行。
●该方法的优点
实现简单;
支持多态——对象角色发生变化,或存在多重角色时;
报表操作实现简单:表中包含了所有信息。
●该方法的缺点
增加类层次中的耦合,类层次中任何类的属性的增加会导致表的变更
如果在某个子类属性的修改错误会影响到整个层次结构,而不仅仅是该子类。
浪费了大量的数据库空间;
可能需要指明具体的角色。
这将失去数据库对Not Null的约束,从数据完整性的角度看,问题很严重。
对关系数据模型进行非常规设计,在数据库表中加入额外的区分子类型的字段。
通过这种方式,可以使关系数据模型支持继承关系和多态。
同时在Hibernate中通常将子类定义为<subclass>的方式来实
409现这个策略。
(3)每个类均映射为数据库表----继承关系树的每个类对应一个表
就是用外键关系来表示继承关系。
在这种策略中,每一个子类,父类都会单独映射到一张表中。
和第一种策略不同的是,这种策略的表中仅包含类中自己定义的那些属性(不包含继承下来的属性)。
为每一个类创建数据库表,表中包含特定于该类的属性和OID 。
下图显示了该方法,注意personOID 作为所有表的主键,它们之间是is-a
的关系。
●
优点
与面向对象的概念的一致性最好-----这种策略最大的好处就是关系模型完全标准化,关系模型和领域模型完全一致。
对多态的支持最好,对于对象所可能的充当的角色仅需要在相应的表中保存记录;
易于修改基类和增加新的类。
●缺点
数据库中存在大量的表
访问数据的时间较长
对报表的支持较差,除非定义视图。
在关系数据模型中用外键参照关系来表示继承关系。
以上每种映射方式都有利有弊,下面介绍每种映射方式的具体实现步骤,同时也还介绍了它们的适用范围。
5、有继承关系的各个子类的构造方法的设计要求在此仅提醒一点,子类的完整构造方法不仅负责初始化子类本身的属性,还应该负责初始化从父类中继承的属性,例如以下是HourlyEmployee 类的构造方法:
public class HourlyEmployee extends Employee {private double rate;/**完整构造方法*/public HourlyEmployee(String name ,double rate,Company company ){super(name,company);this.rate=rate;}/**默认构造方法*/public HourlyEmployee(){}……}Hibernate 只会访问持久化类的默认构造方法,永远不会访问其他形式的构造方法。
提供以上形式的完整构造方法,主要是为Java 应用的编程提供方便。