我的数据模型代表法律实体,如企业或个人。两者都是纳税实体,都有计税表、电话号码和邮寄地址。
我有一个Java模型,它有两个扩展抽象类的具体类。抽象类具有两个具体类共有的属性和集合。
AbstractLegalEntity ConcreteBusinessEntity ConcretePersonEntity
------------------- ---------------------- --------------------
Set<Phone> phones String name String first
Set<Address> addresses BusinessType type String last
String taxId String middle
Address Phone
------- -----
AbsractLegalEntity owner AbstractLegalEntity owner
String street1 String number
String street2
String city
String state
String zip
我在用
冬眠
JPA注释
在一个
MySQL数据库
数据库,类如下:
@MappedSuperclass
public abstract class AbstractLegalEntity {
private Long id; // Getter annotated with @Id @Generated
private Set<Phone> phones = new HashSet<Phone>(); // @OneToMany
private Set<Address> address = new HashSet<Address>(); // @OneToMany
private String taxId;
}
@Entity
public class ConcretePersonEntity extends AbstractLegalEntity {
private String first;
private String last;
private String middle;
}
@Entity
public class Phone {
private AbstractLegalEntity owner; // Getter annotated @ManyToOne @JoinColumn
private Long id;
private String number;
}
问题是
Phone
和
Address
对象需要引用其所有者,这是
AbstractLegalEntity
. Hibernate抱怨道:
@OneToOne or @ManyToOne on Phone references an unknown
entity: AbstractLegalEntity
看起来这是一个相当常见的Java继承场景,所以我希望Hibernate能够支持它。我试着根据
Hibernate forum question
,不再使用
@MappedSuperclass
:
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
但是,现在我得到以下错误。在阅读这个继承映射类型时,看起来我必须使用SEQUENCE而不是IDENTITY,而且MySQL不支持SEQUENCE。
Cannot use identity column key generation with <union-subclass>
mapping for: ConcreteBusinessEntity
当我使用下面的映射时,我在工作方面取得了更大的进展。
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(
name="entitytype",
discriminatorType=DiscriminatorType.STRING
)
我想我应该继续走这条路。我担心的是我把它映射成
@Entity
当我真的不希望抽象实体合法存在的时候。我想知道这是不是正确的方法。对于这种情况,我应该采取什么样的正确方法?