Hibernate继承映射

用户(父类):
public class User implements Serializable {
private Integer id;
private String username;
private String password;
public User() {
super();
}
...此处省略get/set方法...
}

普通用户(子类):
public class Member extends User {
private String name;
private String phone;
public Member() {
super();
}
...此处省略get/set方法...
}

管理员(子类):
public class Manager extends User {
public Manager() {
super();
}
}

映射文件的配置
方式一(只建一张表):
<hibernate-mapping package="day03.pm">
<class name="User" table="t_user1">
<id name="id">
<generator class="native"></generator>
</id>

<discriminator column="status"></discriminator>

<property name="username"></property>
<property name="password"></property>

<subclass name="Member" discriminator-value="me">
<property name="name"></property>
<property name="phone"></property>
</subclass>

<subclass name="Manager" discriminator-value="ma">
</subclass>
</class>
</hibernate-mapping>

mysql> desc t_user1;
+----------+--------------+------+-----+---------+----------------+
| Field    | Type         | Null | Key | Default | Extra          |
+----------+--------------+------+-----+---------+----------------+
| id       | int(11)      | NO   | PRI | NULL    | auto_increment |
| status   | varchar(255) | NO   |     | NULL    |                |
| username | varchar(255) | YES  |     | NULL    |                |
| password | varchar(255) | YES  |     | NULL    |                |
| name     | varchar(255) | YES  |     | NULL    |                |
| phone    | varchar(255) | YES  |     | NULL    |                |
+----------+--------------+------+-----+---------+----------------+

方式二(每个子类各建一张表):
Member.hbm.xml
<hibernate-mapping package="day03.pm">
<import class="User"/>
<class name="Member" table="t_member1">
<id name="id">
<generator class="native"></generator>
</id>
<property name="username"></property>
<property name="password"></property>
<property name="name"></property>
<property name="phone"></property>
</class>
</hibernate-mapping>

Manager.hbm.xml
<hibernate-mapping package="day03.pm">
<import class="User"/>
<class name="Manager" table="t_manager1">
<id name="id">
<generator class="native"></generator>
</id>
<property name="username"></property>
<property name="password"></property>
</class>
</hibernate-mapping>
mysql> desc t_member1;
+----------+--------------+------+-----+---------+----------------+
| Field    | Type         | Null | Key | Default | Extra          |
+----------+--------------+------+-----+---------+----------------+
| id       | int(11)      | NO   | PRI | NULL    | auto_increment |
| username | varchar(255) | YES  |     | NULL    |                |
| password | varchar(255) | YES  |     | NULL    |                |
| name     | varchar(255) | YES  |     | NULL    |                |
| phone    | varchar(255) | YES  |     | NULL    |                |
+----------+--------------+------+-----+---------+----------------+

mysql> desc t_manager1;
+----------+--------------+------+-----+---------+----------------+
| Field    | Type         | Null | Key | Default | Extra          |
+----------+--------------+------+-----+---------+----------------+
| id       | int(11)      | NO   | PRI | NULL    | auto_increment |
| username | varchar(255) | YES  |     | NULL    |                |
| password | varchar(255) | YES  |     | NULL    |                |
+----------+--------------+------+-----+---------+----------------+


方式三(每个类都建表):
User2.hbm.xml
<hibernate-mapping package="day03.pm">
<class name="User" table="t_user2">
<id name="id">
<generator class="native"></generator>
</id>
<property name="username"></property>
<property name="password"></property>

<joined-subclass name="Member" table="t_member2">
<key column="user_id"></key>
<property name="name"></property>
<property name="phone"></property>
</joined-subclass>

<joined-subclass name="Manager" table="t_manager2">
<key column="user_id"></key>
</joined-subclass>

</class>
</hibernate-mapping>

mysql> desc t_user2;
+----------+--------------+------+-----+---------+----------------+
| Field    | Type         | Null | Key | Default | Extra          |
+----------+--------------+------+-----+---------+----------------+
| id       | int(11)      | NO   | PRI | NULL    | auto_increment |
| username | varchar(255) | YES  |     | NULL    |                |
| password | varchar(255) | YES  |     | NULL    |                |
+----------+--------------+------+-----+---------+----------------+


mysql> desc t_member2;
+---------+--------------+------+-----+---------+-------+
| Field   | Type         | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+-------+
| user_id | int(11)      | NO   | PRI | NULL    |       |
| name    | varchar(255) | YES  |     | NULL    |       |
| phone   | varchar(255) | YES  |     | NULL    |       |
+---------+--------------+------+-----+---------+-------+

mysql> desc t_manager2;
+---------+---------+------+-----+---------+-------+
| Field   | Type    | Null | Key | Default | Extra |
+---------+---------+------+-----+---------+-------+
| user_id | int(11) | NO   | PRI | NULL    |       |
+---------+---------+------+-----+---------+-------+

你可能感兴趣的:(Hibernate)