多亏看了这个帖子,要不我整个元旦都不会翻书了……
关于这个问题,为什么需要将entity变成final的呢?换句话说就是final是不可被继承的,那为什么一定要entity变成一个不可被继承的?
在EJB3里面,关于实体继承的部分确实用到了继承,也就是说在某些时候需要继承已有的entity。
举个例子,在数据结构中,肯定会存在某个主表和几个子表,就像表PERSON(主)、CUSTOMER(子,需要继承PERSON)和EMPLOYEE
(子,需要继承CUSTOMER)这样。在这种映射机构中,有一种为A table per subclass,即每个子类一张表。
看这个scheme(摘自Enterprise JavaBeans 3.0)
create table PERSON
(
id
integer primary key not null,
firstNamevarchar(255),
lastNamevarchar(255),
);
create table CUSTOMER
(
id
integer primary key not null,
street
varchar(255),
city
varchar(255),
state
varchar(255),
zip
varchar(255),
);
create table EMPLOYEE
(
EMP_PK
integer primary key not null,
employeeIdInteger,
);
那么对应这种策略,EJB3的处理方式是这样
@Entity
@Inheritance(strategy=InheritanceType.JOINED)
public class Person{
...
}
@Entity
public class Customer extends Person{
...
}
@Entity
@PrimaryKeyJoinColumn(name="EMP_PK")
public class Employee extends Customer{
...
}
于是,这几个entity很明显的需要被继承了,那么entity肯定也就不能被设计为final。
|