1.继承映射
继承映射分为两种情况:简单继承映射和继承映射。
在简单继承映射中,每个子类都要写一个映射文件。
在继承映射中,只使用一个映射文件。继承映射分为三种情况:
所有子类映射到一张表
需要使用鉴别器字段,不符合数据库设计原则
每个类映射到一张表
父类映射到一张表,插入一条子类信息,需要两条sql,父子表之间有外键约束
每个类映射到一张表,父类不映射表
必须使用uuid作为主键类型,所以bean中主键对应的属性不能是int类型
2.简单继承映射
<?xml version="1.0" encoding="utf-8" ?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.juaner.hibernate.extendMap"> <class name="Cat" table="t_cat"> <!--简单继承映射--> <id name="id"> <generator class="native"/> </id> <property name="name" length="25" type="string"/> <property name="catchMouse" length="25"/> </class> </hibernate-mapping>
在这种情况下,获取数据的时候,当写HQL查询时,通过父类查询必须写类的全名:
@Test public void test1(){ Session session = sf.openSession(); session.beginTransaction(); //获取的时候,当写hql查询的时候,通过父类查询必须写上类的全名 Query query = session.createQuery("from com.juaner.hibernate.extendMap.Animal"); List<Animal> list = query.list(); for(Animal animal:list){ System.out.println(animal); } session.getTransaction().commit(); session.close(); }
3.所有子类映射到一张表
<?xml version="1.0" encoding="utf-8" ?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.juaner.hibernate.extendMap"> <class name="Animal" table="t_animal"> <id name="id"> <generator class="native"/> </id> <!--指定鉴别器字段,用于区别子类信息 必须放到id下面,property上面 如果不指定具体值,默认为当前子类的全名 --> <discriminator column="type_"/> <property name="name" length="25" type="string"/> <!--子类信息--> <subclass name="Cat" discriminator-value="cat_"> <property name="catchMouse" length="25"/> </subclass> <subclass name="Monkey" discriminator-value="monkey_"> <property name="eatBanana" length="25"/> </subclass> </class> </hibernate-mapping>
生成的表:
4.每个类映射到一张表
<?xml version="1.0" encoding="utf-8" ?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.juaner.hibernate.extendMap"> <class name="Animal" table="t_animal"> <id name="id"> <generator class="native"/> </id> <property name="name" length="25"/> <!--需要设置外键约束--> <joined-subclass name="Cat" table="t_cat"> <key column="animal_id"></key> <property name="catchMouse" length="25"/> </joined-subclass> <joined-subclass name="Monkey" table="t_monkey"> <key column="animal_id"></key> <property name="eatBanana"/> </joined-subclass> </class> </hibernate-mapping>
执行的sql语句
Hibernate: insert into t_animal (name) values (?) Hibernate: insert into t_cat (catchMouse, animal_id) values (?, ?) Hibernate: insert into t_animal (name) values (?) Hibernate: insert into t_monkey (eatBanana, animal_id) values (?, ?)
5.每个类映射到一张表,父类不映射表
<?xml version="1.0" encoding="utf-8" ?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.juaner.hibernate.extendMap"> <class name="Animal"> <id name="id"> <generator class="uuid"/> </id> <property name="name" length="25"/> <!--每个子类的表的主键为id列,但是主键不能为自增长的--> <union-subclass name="Cat" table="t_cat"> <property name="catchMouse" length="25"/> </union-subclass> <union-subclass name="Monkey" table="t_monkey"> <property name="eatBanana" length="25"/> </union-subclass> </class> </hibernate-mapping>
时间: 2024-09-30 15:07:06