hibernate详解(二)---->>继承关系映射(一)
继承是面对对象编程中一个很重要的特征,在做面对对象的分析和设计时,经常会设计出具体继承关联的持久化类,如何把这些持久化类之间的继承关系映射到关系数据库上的问题已经解决,继承在对象模型中是 is a(是一个)的关系,但是在关系模型中,实体之间只有has a(有一个)的关系,也就是说,继承在对象模型和关系模型上是不匹配的。所幸的是,hibernate提供了3种常见的基继承映射方案。
第一种:整个继承层次一张表
Singer.java
package com.zxf.domain; /** 歌手抽象类 */ public abstract class Singer { private Long id; //对象标识符(OID) private String name; //名称 private String region; //歌手所属区域(如:华人、日韩、欧美等) private String description; //描述 public Singer(){} //无参数的构造方法 //以下为所有属性的getters和setters方法... }
package com.zxf.domain; /** 歌手抽象类 */ public abstract class Singer { private Long id; //对象标识符(OID) private String name; //名称 private String region; //歌手所属区域(如:华人、日韩、欧美等) private String description; //描述 public Singer(){} //无参数的构造方法 //以下为所有属性的getters和setters方法... }
SingleSinger.java
package com.zxf.domain; /** 单人歌手实体类 */ public class SingleSinger extends Singer { private Character gender; //性别 public SingleSinger(){} //无参数的构造方法 //以下为所有属性的getters和setters方法... }
package com.zxf.domain; /** 单人歌手实体类 */ public class SingleSinger extends Singer { private Character gender; //性别 public SingleSinger(){} //无参数的构造方法 //以下为所有属性的getters和setters方法... }
Bands.java
package com.qiujy.domain; /** 乐队组合实体类 */ public class Bands extends Singer { private String leader; //主唱姓名 public Bands(){} //无参数的构造方法 //以下为所有属性的getters和setters方法... }
package com.qiujy.domain; /** 乐队组合实体类 */ public class Bands extends Singer { private String leader; //主唱姓名 public Bands(){} //无参数的构造方法 //以下为所有属性的getters和setters方法... }
Singer.hbm.xml
import java.util.List; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import com.zxf.domain.Bands; import com.zxf.domain.Singer; import com.zxf.domain.SingleSinger; /** * 继承映射测试 * @author z_xiaofei168 */ public class SingerTest { @SuppressWarnings("unchecked") public static void main(String[] args) { //根据hibernate的默认配置文件构建一个SessoinFactory实例 SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); testAdd(sessionFactory); testQuery(sessionFactory); } public static void testAdd(SessionFactory sessionFactory) { //获取Session实例 Session session = sessionFactory.openSession(); //开启事务 session.beginTransaction(); SingleSinger singer = new SingleSinger(); singer.setName("周杰棍"); singer.setRegion("华人"); singer.setDescription("大舌头"); singer.setGender('男'); session.save(singer); Bands bands = new Bands(); bands.setName("花朵乐队"); bands.setRegion("日韩"); bands.setDescription("以口水歌为主"); bands.setLeader("大伟"); session.save(bands); //提交事务 session.getTransaction().commit(); //关闭Session session.close(); } public static void testQuery(SessionFactory sessionFactory) { //获取Session实例 Session session = sessionFactory.openSession(); //开启事务 session.beginTransaction(); List<Singer> singers = session.createQuery("from Singer").list(); for (Singer singer2 : singers) { if(singer2 instanceof SingleSinger){ SingleSinger ss = (SingleSinger)singer2; System.out.println("是单人歌手:" + ss.getName() + ",性别:" + ss.getGender()); }else if(singer2 instanceof Bands){ Bands ban = (Bands)singer2; System.out.println("是乐队组合:" + ban.getName() + ",主唱:" + ban.getLeader()); } } //提交事务 session.getTransaction().commit(); //关闭Session session.close(); } }
import java.util.List; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import com.zxf.domain.Bands; import com.zxf.domain.Singer; import com.zxf.domain.SingleSinger; /** * 继承映射测试 * @author z_xiaofei168 */ public class SingerTest { @SuppressWarnings("unchecked") public static void main(String[] args) { //根据hibernate的默认配置文件构建一个SessoinFactory实例 SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); testAdd(sessionFactory); testQuery(sessionFactory); } public static void testAdd(SessionFactory sessionFactory) { //获取Session实例 Session session = sessionFactory.openSession(); //开启事务 session.beginTransaction(); SingleSinger singer = new SingleSinger(); singer.setName("周杰棍"); singer.setRegion("华人"); singer.setDescription("大舌头"); singer.setGender('男'); session.save(singer); Bands bands = new Bands(); bands.setName("花朵乐队"); bands.setRegion("日韩"); bands.setDescription("以口水歌为主"); bands.setLeader("大伟"); session.save(bands); //提交事务 session.getTransaction().commit(); //关闭Session session.close(); } public static void testQuery(SessionFactory sessionFactory) { //获取Session实例 Session session = sessionFactory.openSession(); //开启事务 session.beginTransaction(); List<Singer> singers = session.createQuery("from Singer").list(); for (Singer singer2 : singers) { if(singer2 instanceof SingleSinger){ SingleSinger ss = (SingleSinger)singer2; System.out.println("是单人歌手:" + ss.getName() + ",性别:" + ss.getGender()); }else if(singer2 instanceof Bands){ Bands ban = (Bands)singer2; System.out.println("是乐队组合:" + ban.getName() + ",主唱:" + ban.getLeader()); } } //提交事务 session.getTransaction().commit(); //关闭Session session.close(); } }
程序的运行结果如下所示:
<script type="text/javascript"></script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script>
相关推荐
guangcheng 2020-09-22
qingjiuquan 2020-07-19
yunzhonmghe 2020-05-19
技术驱动人生 2020-05-03
kevincheung 2020-05-03
踩风火轮的乌龟 2020-04-30
zhengsj 2020-04-15
neverstopforcode 2020-04-14
flydoging 2019-12-09
KDF000 2019-10-18
comxpp 2011-12-11
zhaoshuliok 2011-12-14
nameFay 2012-03-09
surpassdream 2016-01-26
Coohx 2013-10-16
朝军 2017-05-04
张亚京 2019-06-30
acaoye 2019-06-30
技术驱动人生 2016-06-01