继承映射有3种情况:
1.单表继承:每颗继承树使用一个表 table per class hierarchy
2.具体表继承:每个子类一个表 table per subclass
3.类表继承:每个具体类一个表 table per concrete class 有部分限制,
以下,具体表继承:
1.Animal类:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
package com.cnblogs.hibernate_first; public class Animal { private int id; private String name; private boolean sex; 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; } public boolean isSex() { return sex; } public void setSex(boolean sex) { this.sex = sex; } }
2.Bird类:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
package com.cnblogs.hibernate_first; public class Bird extends Animal { private int height; public int getHeight() { return height; } public void setHeight(int height) { this.height = height; } }
3.Pig类
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
package com.cnblogs.hibernate_first; public class Pig extends Animal { private int weight; public int getWeight() { return weight; } public void setWeight(int weight) { this.weight = weight; } }
4.extends.hbm.xml
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
<?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.cnblogs.hibernate_first"> <!-- class的lazy属性,不设置默认为ture 支持延时加载, --> <class name="Animal" table="t_animal"> <id name="id"> <generator class="native"></generator> </id> <property name="name"></property> <property name="sex"></property> <!-- 标签为join-subclass 加入Pig --> <joined-subclass name="Pig" table="t_pig"> <!-- 其中t_pig表中加入一个字段pig 和 t_animal 中的ID对应 --> <key column="pid"></key> <property name="weight"></property> </joined-subclass> <!-- 标签为join-subclass 加入Bird --> <joined-subclass name="Bird" table="t_bird"> <!-- 其中t_pig表中加入一个字段big 和 t_animal 中的ID对应 --> <key column="bid"></key> <property name="height"></property> </joined-subclass> </class> </hibernate-mapping>
5.hibernate.hbm.xml
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <!-- CREATE DATABASE hibernate_extends_1 DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci --> <session-factory> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/hibernate_extends_2?useUnicode=true&characterEncoding=UTF8</property> <property name="connection.username">root</property> <property name="connection.password"></property> <!-- 每次从数据库中取出并放到JDBC的Statement中的记录条数。Fetch Size设的越大,读数据库的次数越少,速度越快,Fetch Size越小,读数据库的次数越多,速度越慢 --> <property name="jdbc.fetch_size">50 </property> <!--批量插入,删除和更新时每次操作的记录数。Batch Size越大,批量操作的向数据库发送Sql的次数越少,速度就越快,同样耗用内存就越大 --> <property name="jdbc.batch_size">23 </property> <!-- SQL 方言 --> <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> <!-- Enable Hibernate's automatic session context management --> <property name="current_session_context_class">thread</property> <!-- 在控制台输出sql语句 --> <property name="show_sql">true</property> <!-- 在启动时根据配置更新数据库 --> <property name="hbm2ddl.auto">update</property> <mapping resource="com/cnblogs/hibernate_first/Extends.hbm.xml" /><!-- 注册我们的实体映射类 --> </session-factory> </hibernate-configuration>