hibernate继承映射之每个具体类一张表
生活随笔
收集整理的這篇文章主要介紹了
hibernate继承映射之每个具体类一张表
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
數據模型
表person
表student
表worker
對象模型
Person private String id;private String name;private int age;private String sex; Student extends Person private String school; Worker extends Person private String factory;?
xml配置:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC"-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.skuedu.bean"><class name="Person" table="person"><id name="id" length="32"><generator class="uuid"></generator></id><property name="name" length="30"></property><property name="age" ></property><property name="sex" length="10"></property><union-subclass name="Student" table="student"><property name="school"></property></union-subclass><union-subclass name="Worker" table="worker" ><property name="factory"></property></union-subclass></class> </hibernate-mapping>測試代碼:
@Testpublic void testSaveStudent(){Session session = HibernateUtil.getSession();Transaction transaction = session.beginTransaction();try{Student student = new Student();student.setAge(20);student.setName("小王");student.setSchool("清華大學");student.setSex("男");session.save(student);transaction.commit();}catch(Exception e){transaction.rollback(); }finally{HibernateUtil.closeSession(session);}}@Testpublic void testSaveWorker(){Session session = HibernateUtil.getSession();Transaction transaction = session.beginTransaction();try{Worker worker = new Worker();worker.setAge(20);worker.setName("小李");worker.setFactory("首鋼");worker.setSex("男");session.save(worker);transaction.commit();}catch(Exception e){transaction.rollback(); }finally{HibernateUtil.closeSession(session);}}@Testpublic void testFind(){}?
?
轉載于:https://www.cnblogs.com/weidan/archive/2013/02/05/2892961.html
總結
以上是生活随笔為你收集整理的hibernate继承映射之每个具体类一张表的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 面积超100个足球场 国内唯一高寒地区自
- 下一篇: 刹车或会失灵!法拉利上演史诗级大召回 超