Hibernate入門(五)
一 Hibernate繼承映射
我們可以將繼承層次結構類與數據庫的表映射。
1. 每個層次類一張表
這個層次結構有三個類,Employee
是Regular_Employee
和Contract_Employee
類的超類(父類)。
此層次結構的表結構如下所示:
XML實現
1)創建持久化類
Employee.java
package cn; public class Employee { private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
Regular_Employee.java
package cn;
public class Regular_Employee extends Employee{
private float salary;
private int bonus;
public float getSalary() {
return salary;
}
public void setSalary(float salary) {
this.salary = salary;
}
public int getBonus() {
return bonus;
}
public void setBonus(int bonus) {
this.bonus = bonus;
}
}
Contract_Employee.java
package cn;
public class Contract_Employee extends Employee{
private float pay_per_hour;
private String contract_duration;
public float getPay_per_hour() {
return pay_per_hour;
}
public void setPay_per_hour(float pay_per_hour) {
this.pay_per_hour = pay_per_hour;
}
public String getContract_duration() {
return contract_duration;
}
public void setContract_duration(String contract_duration) {
this.contract_duration = contract_duration;
}
}
2)創建映射文件
在每個類層次結構一個表的情況下,hibernate框架添加了一個標識符列,該框架指定了記錄的類型。 它主要用於區分記錄。要指定它,必須指定類的 discriminator
子元素。
類的子類subelement
指定子類。 在這種情況下,Regular_Employee
和Contract_Employee
是Employee
類的子類。
Employee.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="cn.Employee" table="employee"> <id name="id"> <generator class="increment"></generator> </id> <discriminator column="type" type="string"></discriminator> <property name="name"></property> <subclass name = "cn.Regular_Employee" discriminator-value="reg_emp"> <property name="salary"></property> <property name="bonus"></property> </subclass> <subclass name = "cn.Contract_Employee" discriminator-value="con_emp"> <property name="pay_per_hour"></property> <property name="contract_duration"></property> </subclass> </class> </hibernate-mapping>
3)在配置文件中增加Employee.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hbm2ddl.auto">update</property>
<property name="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="hibernate.connection.url">
jdbc:mysql://localhost/test
</property>
<property name="hibernate.connection.username">
root
</property>
<property name="hibernate.connection.password">
root
</property>
<property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<property name="show_sql">true</property>
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
<!-- List of XML mapping files -->
<mapping resource="cn/Employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>
4)測試
package cn; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import com.User; public class Test { public static void main(String[] args) { Configuration cfg = new Configuration(); cfg.configure("hibernate.cfg.xml"); SessionFactory factory = cfg.buildSessionFactory(); Session session = factory.openSession(); session.beginTransaction(); Employee emp = new Employee(); emp.setName("emp"); Regular_Employee reg = new Regular_Employee(); reg.setName("reg_emp"); reg.setSalary(10000); reg.setBonus(1); Contract_Employee con = new Contract_Employee(); con.setName("con_emp"); con.setPay_per_hour(200); con.setContract_duration("20h"); session.persist(emp); session.persist(reg); session.persist(con); session.getTransaction().commit(); session.close(); } }
註解實現
1)創建持久化類
Employee.java
package cn; import javax.persistence.Column; import javax.persistence.DiscriminatorColumn; import javax.persistence.DiscriminatorType; import javax.persistence.DiscriminatorValue; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Inheritance; import javax.persistence.InheritanceType; import javax.persistence.Table; @Entity @Table(name="employee") @Inheritance(strategy=InheritanceType.SINGLE_TABLE) @DiscriminatorColumn(name="type",discriminatorType=DiscriminatorType.STRING) @DiscriminatorValue(value="employeee") public class Employee { @Id @GeneratedValue(strategy=GenerationType.AUTO) @Column(name="id") private int id; @Column(name="name") private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
Regular_Employee.java
package cn; import javax.persistence.Column; import javax.persistence.DiscriminatorValue; import javax.persistence.Entity; @Entity @DiscriminatorValue(value="reg") public class Regular_Employee extends Employee{ @Column(name="salary") private float salary; @Column(name="bonus") private int bonus; public float getSalary() { return salary; } public void setSalary(float salary) { this.salary = salary; } public int getBonus() { return bonus; } public void setBonus(int bonus) { this.bonus = bonus; } }
Contract_Employee.java
package cn; import javax.persistence.Column; import javax.persistence.DiscriminatorValue; import javax.persistence.Entity; @Entity @DiscriminatorValue(value="con") 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 pay_per_hour) { this.pay_per_hour = pay_per_hour; } public String getContract_duration() { return contract_duration; } public void setContract_duration(String contract_duration) { this.contract_duration = contract_duration; } }
2)配置文件中增加持久化類
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-configuration SYSTEM "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hbm2ddl.auto">update</property> <property name="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </property> <property name="hibernate.connection.driver_class"> com.mysql.jdbc.Driver </property> <property name="hibernate.connection.url"> jdbc:mysql://localhost/test </property> <property name="hibernate.connection.username"> root </property> <property name="hibernate.connection.password"> root </property> <property name="show_sql">true</property> <mapping class="cn.Employee"/> <mapping class="cn.Regular_Employee"/> <mapping class="cn.Contract_Employee"/> </session-factory> </hibernate-configuration>
3)測試
package cn; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.AnnotationConfiguration; public class Test { public static void main(String[] args) { SessionFactory factory = new AnnotationConfiguration().configure().buildSessionFactory(); Session session = factory.openSession(); Transaction t = session.beginTransaction(); t.begin(); Employee emp = new Employee(); emp.setName("emp"); Regular_Employee reg = new Regular_Employee(); reg.setName("reg_emp"); reg.setSalary(10000); reg.setBonus(1); Contract_Employee con = new Contract_Employee(); con.setName("con_emp"); con.setPay_per_hour(200); con.setContract_duration("20h"); session.persist(emp); session.persist(reg); session.persist(con); t.commit(); session.close(); } }
2. 每個具體類一張表
數據庫中將有三個表,彼此之間沒有關系。
XML實現
1) 創建持久化類
Employee.java
package cnn; public class Employee { private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
Regular_Employee.java
package cnn; public class Regular_Employee extends Employee{ private float salary; private int bonus; public float getSalary() { return salary; } public void setSalary(float salary) { this.salary = salary; } public int getBonus() { return bonus; } public void setBonus(int bonus) { this.bonus = bonus; } }
Contract_Employee.java
package cnn; public class Contract_Employee extends Employee { private float pay_per_hour; private String contract_duration; public float getPay_per_hour() { return pay_per_hour; } public void setPay_per_hour(float pay_per_hour) { this.pay_per_hour = pay_per_hour; } public String getContract_duration() { return contract_duration; } public void setContract_duration(String contract_duration) { this.contract_duration = contract_duration; } }
2)employee.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="cnn.Employee" table="employee"> <id name="id"> <generator class="increment"></generator> </id> <property name="name"></property> <union-subclass name = "cnn.Regular_Employee" table="regular" > <property name="salary"></property> <property name="bonus"></property> </union-subclass> <union-subclass name = "cnn.Contract_Employee" table="contract"> <property name="pay_per_hour"></property> <property name="contract_duration"></property> </union-subclass> </class> </hibernate-mapping>
3) 在配置文件中增加hbm文件的映射
<!-- List of XML mapping files --> <mapping resource="cnn/Employee.hbm.xml"/>
4)測試
package cnn; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import com.User; public class Test { public static void main(String[] args) { Configuration cfg = new Configuration(); cfg.configure("hibernate.cfg.xml"); SessionFactory factory = cfg.buildSessionFactory(); Session session = factory.openSession(); session.beginTransaction(); Employee emp = new Employee(); emp.setName("emp"); Regular_Employee reg = new Regular_Employee(); reg.setName("reg_emp"); reg.setSalary(10000); reg.setBonus(1); Contract_Employee con = new Contract_Employee(); con.setName("con_emp"); con.setPay_per_hour(200); con.setContract_duration("20h"); session.persist(emp); session.persist(reg); session.persist(con); session.getTransaction().commit(); session.close(); } }
註解實現
1)創建持久化類
Employee.java
package cnn; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Inheritance; import javax.persistence.InheritanceType; import javax.persistence.Table; @Entity @Table(name="employee") @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; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
Regular_Employee.java
package cnn; import javax.persistence.AttributeOverride; import javax.persistence.AttributeOverrides; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Table; @Entity @Table(name = "regular") @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; public float getSalary() { return salary; } public void setSalary(float salary) { this.salary = salary; } public int getBonus() { return bonus; } public void setBonus(int bonus) { this.bonus = bonus; } }
Contract_Employee.java
package cnn; import javax.persistence.AttributeOverride; import javax.persistence.AttributeOverrides; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Table; @Entity @Table(name = "contract") @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 pay_per_hour) { this.pay_per_hour = pay_per_hour; } public String getContract_duration() { return contract_duration; } public void setContract_duration(String contract_duration) { this.contract_duration = contract_duration; } }
2)在配置文件中添加hbm文件映射
<mapping class="cnn.Employee"/> <mapping class="cnn.Regular_Employee"/> <mapping class="cnn.Contract_Employee"/>
3)測試類
package cnn; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.AnnotationConfiguration; public class Test { public static void main(String[] args) { SessionFactory factory = new AnnotationConfiguration().configure().buildSessionFactory(); Session session = factory.openSession(); Transaction t = session.beginTransaction(); t.begin(); Employee emp = new Employee(); emp.setName("emp"); Regular_Employee reg = new Regular_Employee(); reg.setName("reg_emp"); reg.setSalary(20000); reg.setBonus(12); Contract_Employee con = new Contract_Employee(); con.setName("con_emp"); con.setPay_per_hour(100); con.setContract_duration("30h"); session.persist(emp); session.persist(reg); session.persist(con); t.commit(); session.close(); } }
3. 每個子類一張表
在每個子類一張表的情況下,子類映射表與主鍵和外鍵的關系與父類映射表相關。
XML實現
1)創建持久化類
Employee.java
package cnnn; public class Employee { private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
Regular_Employee.java
package cnnn; public class Regular_Employee extends Employee{ private float salary; private int bonus; public float getSalary() { return salary; } public void setSalary(float salary) { this.salary = salary; } public int getBonus() { return bonus; } public void setBonus(int bonus) { this.bonus = bonus; } }
Contract_Employee.java
package cnnn; public class Contract_Employee extends Employee{ private float pay_per_hour; private String contract_duration; public float getPay_per_hour() { return pay_per_hour; } public void setPay_per_hour(float pay_per_hour) { this.pay_per_hour = pay_per_hour; } public String getContract_duration() { return contract_duration; } public void setContract_duration(String contract_duration) { this.contract_duration = contract_duration; } }
2)Employee.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="cnnn.Employee" table="employee"> <id name="id"> <generator class="increment"></generator> </id> <property name="name"></property> <joined-subclass name = "cnnn.Regular_Employee" > <key column="reg_id"></key> <property name="salary"></property> <property name="bonus"></property> </joined-subclass> <joined-subclass name = "cnnn.Contract_Employee" > <key column="con_id"></key> <property name="pay_per_hour"></property> <property name="contract_duration"></property> </joined-subclass> </class> </hibernate-mapping>
3)在配置文件中添加hbm文件的映射
<mapping resource="cnnn/Employee.hbm.xml"/>
4)測試
package cnnn; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import com.User; public class Test { public static void main(String[] args) { Configuration cfg = new Configuration(); cfg.configure("hibernate.cfg.xml"); SessionFactory factory = cfg.buildSessionFactory(); Session session = factory.openSession(); session.beginTransaction(); Employee emp = new Employee(); emp.setName("emp"); Regular_Employee reg = new Regular_Employee(); reg.setName("reg_emp"); reg.setSalary(10000); reg.setBonus(1); Contract_Employee con = new Contract_Employee(); con.setName("con_emp"); con.setPay_per_hour(200); con.setContract_duration("20h"); session.persist(emp); session.persist(reg); session.persist(con); session.getTransaction().commit(); session.close(); } }
註解實現
1)創建持久化類
Employee.java
package cnnn; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Inheritance; import javax.persistence.InheritanceType; import javax.persistence.Table; @Entity @Table(name="employee") @Inheritance(strategy = InheritanceType.JOINED) public class Employee { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "id") private int id; @Column(name = "name") private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
Regular_Employee.java
package cnnn; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.PrimaryKeyJoinColumn; import javax.persistence.Table; @Entity @Table(name = "regular") @PrimaryKeyJoinColumn(name = "ID") public class Regular_Employee extends Employee{ @Column(name = "salary") private float salary; @Column(name = "bonus") private int bonus; public float getSalary() { return salary; } public void setSalary(float salary) { this.salary = salary; } public int getBonus() { return bonus; } public void setBonus(int bonus) { this.bonus = bonus; } }
package cnnn; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.PrimaryKeyJoinColumn; import javax.persistence.Table; @Entity @Table(name = "contract") @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; public float getPay_per_hour() { return pay_per_hour; } public void setPay_per_hour(float pay_per_hour) { this.pay_per_hour = pay_per_hour; } public String getContract_duration() { return contract_duration; } public void setContract_duration(String contract_duration) { this.contract_duration = contract_duration; } }
2)在配置文件中增加配置
<mapping class="cnnn.Employee"/> <mapping class="cnnn.Regular_Employee"/> <mapping class="cnnn.Contract_Employee"/>
3)測試
package cnnn; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.AnnotationConfiguration; public class Test { public static void main(String[] args) { SessionFactory factory = new AnnotationConfiguration().configure().buildSessionFactory(); Session session = factory.openSession(); Transaction t = session.beginTransaction(); t.begin(); Employee emp = new Employee(); emp.setName("emp"); Regular_Employee reg = new Regular_Employee(); reg.setName("reg_emp"); reg.setSalary(20000); reg.setBonus(12); Contract_Employee con = new Contract_Employee(); con.setName("con_emp"); con.setPay_per_hour(100); con.setContract_duration("30h"); session.persist(emp); session.persist(reg); session.persist(con); t.commit(); session.close(); } }
Hibernate入門(五)