继承映射(二)每个类一张表JOINED
Company,EmployeeDAO都不变。
Employee变为:
package com.persia.joined; import java.io.Serializable; import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.Table; import javax.persistence.Inheritance; import javax.persistence.InheritanceType; @Entity @Table(name="employee") @Inheritance(strategy=InheritanceType.JOINED) public class Employee implements Serializable { private Integer id; private String name; private Company company; @Id @GeneratedValue public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @ManyToOne(cascade=CascadeType.PERSIST,optional=false) @JoinColumn(name="company_id") public Company getCompany() { return company; } public void setCompany(Company company) { this.company = company; } }
两个继承类:
HourEmployee:
package com.persia.joined; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.PrimaryKeyJoinColumn; import javax.persistence.Table; @Entity @Table(name="HourEmployee") @PrimaryKeyJoinColumn(name="hid")//把主键对应的列名改为"hid" public class HourEmployee extends Employee { private Float rate; @Column(nullable=true) public Float getRate() { return rate; } public void setRate(Float rate) { this.rate = rate; } }
SalaryEmployee:
package com.persia.joined; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Table; import javax.persistence.PrimaryKeyJoinColumn; @Entity @Table(name="SalaryEmployee") @PrimaryKeyJoinColumn(name="sid") public class SalaryEmployee extends Employee { private Float salary; @Column(nullable=true) public Float getSalary() { return salary; } public void setSalary(Float salary) { this.salary = salary; } }
--------------------------------------------------------------------------------------------------------------------------
生成的数据库表:
mysql> desc company; +-------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+--------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | varchar(255) | YES | | NULL | | +-------+--------------+------+-----+---------+----------------+ 2 rows in set (0.06 sec) mysql> desc employee; +------------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +------------+--------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | varchar(255) | YES | | NULL | | | company_id | int(11) | NO | MUL | NULL | | +------------+--------------+------+-----+---------+----------------+ 3 rows in set (0.00 sec) mysql> desc houremployee; +-------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+-------+ | hid | int(11) | NO | PRI | NULL | | | rate | float | YES | | NULL | | +-------+---------+------+-----+---------+-------+ 2 rows in set (0.00 sec) mysql> desc salaryemployee; +--------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+---------+------+-----+---------+-------+ | sid | int(11) | NO | PRI | NULL | | | salary | float | YES | | NULL | | +--------+---------+------+-----+---------+-------+ 2 rows in set (0.00 sec)
----------------------------------------------------------------------------------------------------------------------------------
插入数据:
mysql> select * from company; +----+----------+ | id | name | +----+----------+ | 1 | waltmart | +----+----------+ 1 row in set (0.00 sec) mysql> select * from employee; +----+----------+------------+ | id | name | company_id | +----+----------+------------+ | 1 | salary-a | 1 | | 2 | hour-b | 1 | +----+----------+------------+ 2 rows in set (0.00 sec) mysql> select * from houremployee; +-----+------+ | hid | rate | +-----+------+ | 2 | 888 | +-----+------+ 1 row in set (0.00 sec) mysql> select * from salaryemployee; +-----+--------+ | sid | salary | +-----+--------+ | 1 | 9999 | +-----+--------+ 1 row in set (0.00 sec)
--------------------------------------------------------------------------------------------------------------------------------------------
修改HourEmployee的数据,就houremployee表的数据改变:
mysql> select * from houremployee; +-----+------+ | hid | rate | +-----+------+ | 2 | 95 | +-----+------+ 1 row in set (0.00 sec)
-------------------------------------------------------------------------------------------------------------------------------------
删除SalaryEmployee:
mysql> select * from company; +----+----------+ | id | name | +----+----------+ | 1 | waltmart | +----+----------+ 1 row in set (0.00 sec) mysql> select * from employee; +----+--------+------------+ | id | name | company_id | +----+--------+------------+ | 2 | hour-b | 1 | +----+--------+------------+ 1 row in set (0.00 sec) mysql> select * from houremployee; +-----+------+ | hid | rate | +-----+------+ | 2 | 95 | +-----+------+ 1 row in set (0.00 sec) mysql> select * from salaryemployee; Empty set (0.00 sec)
删除后父类跟子类的表都删除了相关记录。
--------------------------------------------------------------------------------------------------------------------------
删除Company:
mysql> select * from salaryemployee; Empty set (0.00 sec) mysql> select * from company; Empty set (0.00 sec) mysql> select * from employee; Empty set (0.00 sec) mysql> select * from salaryemployee; Empty set (0.00 sec) mysql> select * from houremployee; Empty set (0.00 sec)
全部级联删除。
——————————————————————————————————————————————————————----------
优点:
1.支持多态关联和查询
2.允许子类的属性定义not null
缺点:
1.查询性能不如SINGLE_TABLE
2.当对查询性能要求不高,子类属性非常多,而且需要对其子类属性定义not null 用此策略。
浙公网安备 33010602011771号