hibernate继承映射2

来源:互联网 发布:windows update卡在100 编辑:程序博客网 时间:2024/06/06 02:44

       上一篇博客的继承映射所用到的节点是joined-subclass节点,在数据库端生成的是三个表,而该篇所用到的节点是union-subclass节点,生成的表只有两个。但此种方法生成的表的主键不能进行自增长。

1、创建实体类Animal.java

package cn.itcast.b;public class Animal {private String id;private String name;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getId() {return id;}public void setId(String id) {this.id = id;}}

2、创建实体类Cat.java继承于Animal类

package cn.itcast.b;public class Cat extends Animal{private String catchMouse;public String getCatchMouse() {return catchMouse;}public void setCatchMouse(String catchMouse) {this.catchMouse = catchMouse;}}


3、创建实体类Monkey.java继承于Animal类

package cn.itcast.b;public class Monkey extends Animal{private String eatBanana;public String getEatBanana() {return eatBanana;}public void setEatBanana(String eatBanana) {this.eatBanana = eatBanana;}}

4、配置Aminal.hbm.xml

<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping package="cn.itcast.b"><!-- abstract="true"指定实体类对象不对应表,即在数据库端不生成表 --><class name="Animal" abstract="true"><!-- 如果用union-subclass节点,主键生成策略不能为自增长--><id name="id"><generator class="uuid"></generator></id><!-- 其他字段 --><property name="name" length="20"></property><!-- 子类:猫  t_catunion-subclass  table 指定为表名, 表的主键即为id列--><union-subclass name="Cat" table="t_cat"><property name="catchMouse"></property></union-subclass><!-- 子类:猴子  t_monkey --><union-subclass name="Monkey" table="t_monkey"><property name="eatBanana"></property></union-subclass></class></hibernate-mapping>

5、配置hibernate.cfg.xml

<!DOCTYPE hibernate-configuration PUBLIC"-//Hibernate/Hibernate Configuration DTD 3.0//EN""http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><hibernate-configuration><session-factory><property name="show_sql">true</property><!-- 数据库连接配置 --><property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property><property name="hibernate.connection.url">jdbc:mysql:///employee</property><property name="hibernate.connection.username">root</property><property name="hibernate.connection.password">123456</property><property name="dialect">org.hibernate.dialect.MySQL5Dialect</property><!-- 其他相关配置 --><!-- 显示hibernate在运行时执行的sql语句 --><property name="hibernate.show_sql">true</property><!-- 格式化sql --><property name="hibernate.format_sql">true</property><!-- 自动建表 --><property name="hibernate.hbm2ddl.auto">update</property><!-- 加载所有映射 --><mapping resource="cn/itcast/b/Animal.hbm.xml"/></session-factory></hibernate-configuration>

6、创建测试类App.java

package cn.itcast.b;import static org.junit.Assert.*;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;import org.junit.Test;public class App {private static SessionFactory sf;static {sf=new Configuration().configure().buildSessionFactory();}@Testpublic void test() {Session session=sf.openSession();session.beginTransaction();Cat cat = new Cat();cat.setName("小猫");cat.setCatchMouse("抓猫");Monkey monkey = new Monkey();monkey.setName("猴子");monkey.setEatBanana("吃香蕉");session.save(monkey);session.save(cat);session.getTransaction().commit();session.close();}}


原创粉丝点击