public class Animal { private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }Bird子类:
public class Bird extends Animal { private int height; public int getHeight() { return height; } public void setHeight(int height) { this.height = height; } }Pig子类:
public class Pig extends Animal { private int weight; public int getWeight() { return weight; } public void setWeight(int weight) { this.weight = weight; } }Extends.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.zero.hibernate.vo"> <class name="Animal" table="t_animal" lazy="false"> <id name="id"> <generator class="native" /> </id> <discriminator column="type" type="string" /> <!-- 鉴别标签,会在 t_animal表加type字段 --> <property name="name" /> <property name="sex" /> <subclass name="Pig" discriminator-value="Pig"> <!-- discriminator-value 鉴别值 --> <property name="weight" /> </subclass> <subclass name="Bird" discriminator-value="Bird"> <!-- discriminator-value 鉴别值 --> <property name="height" /> </subclass> </class> </hibernate-mapping>生成的建表语句:
drop table if exists t_animal create table t_animal ( id integer not null auto_increment, type varchar(255) not null, name varchar(255), weight integer, height integer, primary key (id) )测试:
Bird bird = new Bird(); bird.setName("鸟"); bird.setHeight(10); session.save(bird); Pig pig = new Pig(); pig.setName("猪"); pig.setWeight(100); session.save(pig);
Animal animal = (Animal)session.load(Animal.class, 1); if (animal instanceof Pig) { Pig pig = (Pig) animal; System.out.println(pig.getWeight()); }else { Bird bird = (Bird) animal; System.out.println(bird.getHeight()); }
List animalList = session.createQuery("from Animal").list(); for (Iterator iter=animalList.iterator(); iter.hasNext();) { Animal animal = (Animal)iter.next(); //采用hql查询返回的是真正的类型,所以hql支持多态查询 if (animal instanceof Pig) { System.out.println(animal.getName()); }else if (animal instanceof Bird) { System.out.println(animal.getName()); } }
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.zero.hibernate.vo"> <class name="Animal" table="t_animal" abstract="true"><!-- abstract="true"将不会生成表 --> <id name="id"> <generator class="assigned" /><!-- 手动分配 id --> </id> <property name="name" /> <union-subclass name="Pig" table="t_pig"> <property name="weight" /> </union-subclass> <union-subclass name="Bird" table="t_bird"> <property name="height" /> </union-subclass> </class> </hibernate-mapping>生成的建表语句:
drop table if exists t_bird drop table if exists t_pig create table t_bird ( id integer not null, name varchar(255), height integer, primary key (id) ) create table t_pig ( id integer not null, name varchar(255), weight integer, primary key (id) )测试:
Pig pig = new Pig(); pig.setId(1); pig.setName("猪"); pig.setWeight(100); session.save(pig); Bird bird = new Bird(); bird.setId(2); bird.setName("鸟"); bird.setHeight(10); session.save(bird); //Hibernate: insert into t_pig (name, weight, id) values (?, ?, ?) //Hibernate: insert into t_bird (name, height, id) values (?, ?, ?)
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.zero.hibernate.vo"> <class name="Animal" table="t_animal"> <id name="id"> <generator class="native" /> </id> <property name="name" /> <joined-subclass name="Pig" table="t_pig"> <key column="pigId" /> <property name="weight" /> </joined-subclass> <joined-subclass name="Bird" table="t_bird"> <key column="birdId" /> <property name="height" /> </joined-subclass> </class> </hibernate-mapping>生成的建表语句:
alter table t_bird drop foreign key FK_ihcyneoc7iub1oyax1p781ly1 alter table t_pig drop foreign key FK_2cjrm0mijyj0mul5hqeck0v36 drop table if exists t_animal drop table if exists t_bird drop table if exists t_pig create table t_animal ( id integer not null auto_increment, name varchar(255), primary key (id) ) create table t_bird ( birdId integer not null, height integer, primary key (birdId) ) create table t_pig ( pigId integer not null, weight integer, primary key (pigId) ) alter table t_bird add constraint FK_ihcyneoc7iub1oyax1p781ly1 foreign key (birdId) references t_animal (id) alter table t_pig add constraint FK_2cjrm0mijyj0mul5hqeck0v36 foreign key (pigId) references t_animal (id)测试:
Pig pig = new Pig(); pig.setName("猪"); pig.setWeight(100); session.save(pig); Bird bird = new Bird(); bird.setName("鸟"); bird.setHeight(10); session.save(bird); //Hibernate: insert into t_animal (name) values (?) //Hibernate: insert into t_pig (weight, pigId) values (?, ?) //Hibernate: insert into t_animal (name) values (?) //Hibernate: insert into t_bird (height, birdId) values (?, ?)