📅  最后修改于: 2021-01-02 13:43:37             🧑  作者: Mango
如果使用“每混凝土表”类,则按类创建表。因此,表中没有可为空的值。这种方法的缺点是在子类表中创建了重复的列。
在这里,我们需要在父类中使用@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)批注,在子类中使用@AttributeOverrides批注。
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)指定我们使用每个具体类策略的表。仅应在父类中指定。
@AttributeOverrides定义父类属性将在此类中被覆盖。在表结构中,父类表列将添加到子类表中。
类层次结构如下:
每个表的表结构如下:
在此示例中,我们将创建三个类,并在employee.hbm.xml文件中提供这些类的映射。
您需要创建代表继承的持久类。让我们为上述层次结构创建三个类:
package com.javatpoint.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
}
package com.javatpoint.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
}
package com.javatpoint.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;
}
}
打开pom.xml文件,然后单击源。现在,在
org.hibernate
hibernate-core
5.3.1.Final
com.oracle
ojdbc14
10.2.0.4.0
update
org.hibernate.dialect.Oracle9Dialect
jdbc:oracle:thin:@localhost:1521:xe
system
jtp
oracle.jdbc.driver.OracleDriver
定义了hbm2ddl.auto属性,用于在数据库中创建自动表。
在此类中,我们仅将雇员对象存储在数据库中。
package com.javatpoint.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");
}
}