Hibernate教程-使用注解实现每个具体类的表
![](https://wx.javatiku.cn/j/j301-t.jpg)
在每个具体类的表模式中,每个类都会创建一个独立的表。因此,表中没有可空值。该方法的缺点是子类表中会创建重复的列。
在这里,我们需要在父类中使用 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
注解,并在子类中使用 @AttributeOverrides
注解。
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) 指定我们使用的是每个具体类一个表的策略。该注解只应在父类中指定。
@AttributeOverrides 定义父类的属性将在该类中重写。在表结构中,父类表的列将添加到子类表中。
类层次结构如下所示:
每个表的表结构如下:
Employee 类的表结构
Regular_Employee 类的表结构
Contract_Employee 类的表结构
每个具体类的表的示例
在这个示例中,我们将创建三个类,并在 employee.hbm.xml 文件中提供这些类的映射。
1) 创建持久化类
你需要创建表示继承关系的持久化类。让我们为上述层次结构创建三个类:
文件: Employee.java
package cn.javatiku.mypackage;
import javax.persistence.*;
@Entity
@Table(name = "employee102")
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
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 = "regularemployee102")
@AttributeOverrides({
@AttributeOverride(name = "id", column = @Column(name = "id")),
@AttributeOverride(name = "name", column = @Column(name = "name"))
})
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.javatiku.mypackage;
import javax.persistence.*;
@Entity
@Table(name = "contractemployee102")
@AttributeOverrides({
@AttributeOverride(name = "id", column = @Column(name = "id")),
@AttributeOverride(name = "name", column = @Column(name = "name"))
})
public class Contract_Employee extends Employee {
@Column(name = "pay_per_hour")
private float pay_per_hour;
@Column(name = "contract_duration")
private String contract_duration;
public float getPay_per_hour() {
return pay_per_hour;
}
public void setPay_per_hour(float payPerHour) {
pay_per_hour = payPerHour;
}
public String getContract_duration() {
return contract_duration;
}
public void setContract_duration(String contractDuration) {
contract_duration = contractDuration;
}
}
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) 在配置文件中添加 hbm 文件的映射
文件: 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
类将三个员工对象持久化到数据库中,各自存储在对应的表中。