这里详细讨论继承映射的3种方式:
1.Table per concrete class
继承关系如下图:
数据表设计如下图:
MySQL数据库中执行如下DDL:
- CREATE TABLE defaultuser (
- id INT(11) NOT NULL auto_increment PRIMARY KEY,
- name VARCHAR(100) NOT NULL default '',
- someProperty VARCHAR(100)
- );
- CREATE TABLE poweruser (
- id INT(11) NOT NULL auto_increment PRIMARY KEY,
- name VARCHAR(100) NOT NULL default '',
- otherProperty VARCHAR(100)
- );
User.java
- package com.hb3.pack_08.model;
- public class User {
- private Integer id;
- private String name;
-
- public User() {
- }
- public Integer getId() {
- return id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- }
DefaultUser.java
- package com.hb3.pack_08.model;
- public class DefaultUser extends User {
- private String someProperty;
- public DefaultUser() {
- }
- public String getSomeProperty() {
- return someProperty;
- }
- public void setSomeProperty(String someProperty) {
- this.someProperty = someProperty;
- }
- }
PowerUser.java
- package com.hb3.pack_08.model;
- public class PowerUser extends User {
- private String otherProperty;
-
- public PowerUser() {
- }
- public String getOtherProperty() {
- return otherProperty;
- }
- public void setOtherProperty(String otherProperty) {
- this.otherProperty = otherProperty;
- }
- }
DefaultUser.hbm.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>
- <class name="com.hb3.pack_08.model.DefaultUser" table="defaultuser">
-
- <id name="id" column="id" type="java.lang.Integer">
- <generator class="native"/>
- </id>
-
- <property name="name" column="name" type="java.lang.String"/>
-
- <property name="someProperty" column="someProperty" type="java.lang.String"/>
-
- </class>
-
- </hibernate-mapping>
PowerUser.hbm.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>
- <class name="com.hb3.pack_08.model.PowerUser" table="poweruser">
-
- <id name="id" column="id" type="java.lang.Integer">
- <generator class="native"/>
- </id>
-
- <property name="name" column="name" type="java.lang.String"/>
-
- <property name="otherProperty" column="otherProperty" type="java.lang.String"/>
-
- </class>
-
- </hibernate-mapping>
修改hibernate.cfg.xml文件:
......
<mapping resource="com/hb3/pack_08/model/DefaultUser.hbm.xml" />
<mapping resource="com/hb3/pack_08/model/PowerUser.hbm.xml" />
......
- package com.hb3.pack_08;
- import java.io.IOException;
- import java.sql.SQLException;
- import java.util.Iterator;
- import org.hibernate.Query;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.Transaction;
- import org.hibernate.cfg.Configuration;
- import com.hb3.pack_08.model.DefaultUser;
- import com.hb3.pack_08.model.PowerUser;
- import com.hb3.pack_08.model.User;
- public class BusinessService {
- public static void main(String[] args) throws IOException, SQLException {
-
- Configuration config = new Configuration().configure();
- SessionFactory sessionFactory = config.buildSessionFactory();
- Session session = sessionFactory.openSession();
-
- DefaultUser defaultUser = new DefaultUser();
- defaultUser.setName("chenyan");
- defaultUser.setSomeProperty("hu....hu...");
-
- PowerUser powerUser = new PowerUser();
- powerUser.setName("shenbin");
- powerUser.setOtherProperty("Bla...Bla...");
- Transaction tx = session.beginTransaction();
- session.save(powerUser);
- session.save(defaultUser);
- tx.commit();
-
-
- session.close();
- session = sessionFactory.openSession();
-
-
- Query query = session.createQuery("from com.hb3.pack_08.model.User");
- Iterator<?> iterator = query.list().iterator();
- while(iterator.hasNext()) {
- User user = (User) iterator.next();
- System.out.println(user.getName());
- }
-
-
- session.close();
- sessionFactory.close();
- }
- }
请注意:查询中查询的是User,所以会找出所有DefaultUser和PowerUser的资料。User类中不包括的他子类特有的字段也会被找到。
值得一提的是,查询中如果直接使用"from java.lang.Object"这样的HQL的话,将查出数据库中所有的数据资料。因为Object是Java中所有类的父类。
2.Table per class hierarchy
数据表设计如下图:
DDL文件:
- CREATE TABLE user (
- id INT(11) NOT NULL auto_increment PRIMARY KEY,
- userType VARCHAR(50) NOT NULL,
- name VARCHAR(100) NOT NULL default '',
- someProperty VARCHAR(100),
- otherProperty VARCHAR(100)
- );
POJO同上,分为:User,PowerUser,DefaultUser。
User.hbm.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>
- <class name="com.hb3.pack_09.model.User" table="user">
-
- <id name="id" column="id" type="java.lang.Integer">
- <generator class="native"/>
- </id>
-
- <discriminator column="userType" type="java.lang.String"/>
-
- <property name="name" column="name" type="java.lang.String"/>
-
- <subclass name="com.hb3.pack_09.model.DefaultUser"
- discriminator-value="Default">
-
- <property name="someProperty"
- column="someProperty"
- type="java.lang.String"/>
- </subclass>
-
- <subclass name="com.hb3.pack_09.model.PowerUser"
- discriminator-value="Power">
-
- <property name="otherProperty"
- column="otherProperty"
- type="java.lang.String"/>
- </subclass>
-
- </class>
-
- </hibernate-mapping>
修改hibernate.cfg.xml文件:
......
<mapping resource="com/hb3/pack_09/model/User.hbm.xml" />
......
测试代码也和上面的基本一致:
- package com.hb3.pack_09;
- import java.io.IOException;
- import java.sql.SQLException;
- import java.util.Iterator;
- import org.hibernate.Query;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.Transaction;
- import org.hibernate.cfg.Configuration;
- import com.hb3.pack_09.model.DefaultUser;
- import com.hb3.pack_09.model.PowerUser;
- import com.hb3.pack_09.model.User;
- public class BusinessService {
- public static void main(String[] args) throws IOException, SQLException {
-
- Configuration config = new Configuration().configure();
- SessionFactory sessionFactory = config.buildSessionFactory();
- Session session = sessionFactory.openSession();
-
- DefaultUser defaultUser = new DefaultUser();
- defaultUser.setName("chenyan");
- defaultUser.setSomeProperty("hu....hu...");
-
- PowerUser powerUser = new PowerUser();
- powerUser.setName("shenbin");
- powerUser.setOtherProperty("Bla...Bla...");
- Transaction tx = session.beginTransaction();
- session.save(powerUser);
- session.save(defaultUser);
- tx.commit();
-
-
- session.close();
- session = sessionFactory.openSession();
-
-
- Query query = session.createQuery("from com.hb3.pack_09.model.User");
- Iterator<?> iterator = query.list().iterator();
- while(iterator.hasNext()) {
- User user = (User) iterator.next();
- System.out.println(user.getName());
- }
-
-
- session.close();
- sessionFactory.close();
- }
- }
3.Table per subclass
数据表设计如下图:
DDL文件:
- CREATE TABLE user (
- id INT(11) NOT NULL auto_increment PRIMARY KEY,
- name VARCHAR(100) NOT NULL default ''
- );
- CREATE TABLE defaultuser (
- id INT(11) NOT NULL PRIMARY KEY,
- someProperty VARCHAR(100)
- );
- CREATE TABLE poweruser (
- id INT(11) NOT NULL PRIMARY KEY,
- otherProperty VARCHAR(100)
- );
POJO同上,分为:User,PowerUser,DefaultUser。
User.hbm.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>
- <class name="com.hb3.pack_10.model.User" table="user">
-
- <id name="id" column="id" type="java.lang.Integer">
- <generator class="native"/>
- </id>
-
- <property name="name" column="name" type="java.lang.String"/>
- <joined-subclass name="com.hb3.pack_10.model.DefaultUser"
- table="defaultuser">
-
- <key column="id"/>
-
- <property name="someProperty"
- column="someProperty"
- type="java.lang.String"/>
-
- </joined-subclass>
-
- <joined-subclass name="com.hb3.pack_10.model.PowerUser"
- table="poweruser">
-
- <key column="id"/>
-
- <property name="otherProperty"
- column="otherProperty"
- type="java.lang.String"/>
-
- </joined-subclass>
-
- </class>
-
- </hibernate-mapping>
修改hibernate.cfg.xml文件:
......
<mapping resource="com/hb3/pack_10/model/User.hbm.xml" />
......
测试代码如下:
- package com.hb3.pack_10;
- import java.io.IOException;
- import java.sql.SQLException;
- import java.util.Iterator;
- import org.hibernate.Query;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.Transaction;
- import org.hibernate.cfg.Configuration;
- import com.hb3.pack_10.model.DefaultUser;
- import com.hb3.pack_10.model.PowerUser;
- import com.hb3.pack_10.model.User;
- public class BusinessService {
- public static void main(String[] args) throws IOException, SQLException {
-
- Configuration config = new Configuration().configure();
- SessionFactory sessionFactory = config.buildSessionFactory();
- Session session = sessionFactory.openSession();
-
- DefaultUser defaultUser = new DefaultUser();
- defaultUser.setName("chenyan");
- defaultUser.setSomeProperty("hu....hu...");
-
- PowerUser powerUser = new PowerUser();
- powerUser.setName("shenbin");
- powerUser.setOtherProperty("Bla...Bla...");
- Transaction tx = session.beginTransaction();
- session.save(powerUser);
- session.save(defaultUser);
- tx.commit();
-
-
- session.close();
- session = sessionFactory.openSession();
-
-
- Query query = session.createQuery("from com.hb3.pack_10.model.User");
- Iterator<?> iterator = query.list().iterator();
- while(iterator.hasNext()) {
- User user = (User) iterator.next();
- System.out.println(user.getName());
- }
-
-
- session.close();
- sessionFactory.close();
- }
- }
ExtJS教程-
Hibernate教程-
Struts2 教程-
Lucene教程