Hibernate教程-使用注解实现每个子类的表
正如我们之前所述,在每个子类的表策略中,表是按照持久化类创建的,但它们通过主键和外键关系进行处理。因此,在关系中不会有重复的列。
我们需要在父类中指定 @Inheritance(strategy=InheritanceType.JOINED) 注解,并在子类中使用 @PrimaryKeyJoinColumn 注解。
让我们看一下将要映射的类的层次结构。
每个表的表结构如下:
Employee 类的表结构
Regular_Employee 类的表结构
Contract_Employee 类的表结构
使用注解实现每个子类的表的示例
在这个示例中,我们将创建三个类,并在 employee.hbm.xml 文件中提供这些类的映射。
1) 创建持久化类
你需要创建表示继承关系的持久化类。让我们为上述层次结构创建三个类:
文件: Employee.java
package cn.javatiku.mypackage;
import javax.persistence.*;
@Entity
@Table(name = "employee103")
@Inheritance(strategy = InheritanceType.JOINED)
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private int id;
@Column(name = "name")
private String name;
// setters and getters
}
文件: Regular_Employee.java
package cn.javatiku.mypackage;
import javax.persistence.*;
@Entity
@Table(name = "regularemployee103")
@PrimaryKeyJoinColumn(name = "ID")
public class Regular_Employee extends Employee {
@Column(name = "salary")
private float salary;
@Column(name = "bonus")
private int bonus;
// setters and getters
}
文件: Contract_Employee.java
package cn.javatikut.mypackage;
import javax.persistence.*;
@Entity
@Table(name = "contractemployee103")
@PrimaryKeyJoinColumn(name = "ID")
public class Contract_Employee extends Employee {
@Column(name = "pay_per_hour")
private float pay_per_hour;
@Column(name = "contract_duration")
private String contract_duration;
// setters and getters
}
2) 在 pom.xml 文件中添加项目信息和配置
打开 pom.xml 文件并点击 source。现在,在 <dependencies>....</dependencies>
标签之间添加以下依赖项。
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.3.1.Final</version>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc14</artifactId>
<version>10.2.0.4.0</version>
</dependency>
3) 创建配置文件
打开 hibernate.cfg.xml 文件,并添加映射资源条目,如下所示:
<mapping class="cn.javatiku.mypackage.Employee"/>
<mapping class="cn.javatiku.mypackage.Contract_Employee"/>
<mapping class="cn.javatiku.mypackage.Regular_Employee"/>
现在配置文件将如下所示:
文件: hibernate.cfg.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 5.3//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-5.3.dtd">
<!-- 由 MyEclipse Hibernate 工具生成 -->
<hibernate-configuration>
<session-factory>
<property name="hbm2ddl.auto">update</property>
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
<property name="connection.username">system</property>
<property name="connection.password">jtp</property>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<mapping class="cn.javatiku.mypackage.Employee"/>
<mapping class="cn.javatiku.mypackage.Contract_Employee"/>
<mapping class="cn.javatiku.mypackage.Regular_Employee"/>
</session-factory>
</hibernate-configuration>
hbm2ddl.auto 属性定义用于在数据库中自动创建表。
4) 创建存储持久化对象的类
在这个类中,我们将简单地将员工对象存储在数据库中。
文件: StoreData.java
package cn.javatiku.mypackage;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
public class StoreData {
public static void main(String args[]) {
StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();
Metadata meta = new MetadataSources(ssr).getMetadataBuilder().build();
SessionFactory factory = meta.getSessionFactoryBuilder().build();
Session session = factory.openSession();
Transaction t = session.beginTransaction();
Employee e1 = new Employee();
e1.setName("Gaurav Chawla");
Regular_Employee e2 = new Regular_Employee();
e2.setName("Vivek Kumar");
e2.setSalary(50000);
e2.setBonus(5);
Contract_Employee e3 = new Contract_Employee();
e3.setName("Arjun Kumar");
e3.setPay_per_hour(1000);
e3.setContract_duration("15 hours");
session.persist(e1);
session.persist(e2);
session.persist(e3);
t.commit();
session.close();
System.out.println("success");
}
}
运行 StoreData
类将三个员工对象持久化到数据库中,各自存储在对应的表中。