问题描述
我的数据模型代表法律实体,例如商业或个人。两者都是纳税实体,两者都有TaxID,一系列电话号码和一系列邮寄地址。我有一个包含两个具体类的Java模型这扩展了一个抽象类。抽象类具有属性和集合,这些属性和集合对于两个具体类都是通用的。
AbstractLegalEntity ConcreteBusinessEntity ConcretePersonEntity
---- --------------- ---------------------- ------------- -------
Set< Phone>电话字符串名称字符串首先
Set< Address>地址BusinessType类型字符串最后
字符串taxId字符串中间
地址电话
------- -----
AbsractLegalEntity所有者AbstractLegalEntity所有者
字符串street1字符串编号
字符串street2
字符串城市
字符串状态
字符串zip
我在 JPA注释,并使用类如下:
@MappedSuperclass
public abstract class AbstractLegalEntity {
private Long id; // Getter用@Id注解@Generated
private Set< Phone> phones = new HashSet< Phone>(); // @OneToMany
私人设置<地址> address = new HashSet< Address>(); // @OneToMany
private String taxId;
}
@Entity
公共类ConcretePersonEntity扩展AbstractLegalEntity {
私有字符串;
私人字符串最后;
私有字符串中间;
}
@实体
公共类电话{
private AbstractLegalEntity owner; // Getter注解@ManyToOne @JoinColumn
私有长ID;
私人字符串编号;
$ / code>
问题在于手机
和地址
对象需要引用它们的所有者,这是 AbstractLegalEntity
。 Hibernate抱怨:
@OneToOne或@ManyToOne on Phone引用未知的
实体:AbstractLegalEntity
code>
看起来这是一个相当常见的Java继承场景,所以我希望Hibernate能支持它。我试着根据 Hibernate论坛问题更改AbstractLegalEntity的映射。 a>,不再使用 @MappedSuperclass
:
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
但是,现在我得到以下错误。在阅读这种继承映射类型时,看起来我必须使用SEQUENCE而不是IDENTITY,而MySQL不支持SEQUENCE。
不能使用< union-subclass>标识列密钥生成
映射为:ConcreteBusinessEntity
在使用我的工作时,我正在取得更多进展以下映射。
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(
name =entitytype,
discriminatorType = DiscriminatorType.STRING
)
I我想我应该继续走下去。我担心的是,当我真的不希望AbstractLegalEntity的实例存在时,我将它映射为 @Entity
。我想知道这是否是正确的方法。
使用:
$ b我应该采取的正确方法是什么? $ b
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
AbstractLegalEntity
然后在数据库中,您将拥有一个AbstractLegalEntity表和AbstractLegalEntity类的表。如果AbstractLegalEntity是抽象的,则不会有实例。可以在这里使用多态性。
当您使用:
@ MappedSuperclass
AbstractLegalEntity
@Entity
ConcretePersonEntity extends AbstractLegalEntity
它在数据库中创建一个表ConcretePersonEntity,但使用两个类的列。
My data model represents legal entities, such as a Business or a Person. Both are tax-paying entities, and both have a TaxID, a collection of phone numbers, and a collection of mailing addresses.
I have a Java model with two concrete classes that extend an abstract class. The abstract class has properties and collections that are common to both concrete classes.
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
I'm using JPA Annotations on a MySQL database, with classes like this:
@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;
}
The problem is that Phone
and Address
objects need to refer to their owner, which is an AbstractLegalEntity
. Hibernate complains:
@OneToOne or @ManyToOne on Phone references an unknown
entity: AbstractLegalEntity
It seems like this would be a fairly common Java inheritance scenario, so I hope that Hibernate would support it. I've tried changing the mapping for AbstractLegalEntity based on a Hibernate forum question, no longer using @MappedSuperclass
:
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
However, now I get the following error. When reading up on this inheritance mapping type, it looks like I have to use SEQUENCE not IDENTITY, and MySQL doesn't support SEQUENCE.
Cannot use identity column key generation with <union-subclass>
mapping for: ConcreteBusinessEntity
I'm making more progress toward getting things working when I use the following mapping.
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(
name="entitytype",
discriminatorType=DiscriminatorType.STRING
)
I'm thinking I should continue down this path. My concern is that I'm mapping it as an @Entity
when I really don't ever want an instance of AbstractLegalEntity to ever exist. I'd like to know if this is the right approach. What is the correct approach I should be taking for this situation?
Use:
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
AbstractLegalEntity
Then in database you will have one table for AbstractLegalEntity and tables for classes which extends AbstractLegalEntity class. You won't have instances of AbstractLegalEntity if it's abstract. Polymorphism can be here used.
When you use:
@MappedSuperclass
AbstractLegalEntity
@Entity
ConcretePersonEntity extends AbstractLegalEntity
It creates in database just one table ConcretePersonEntity but with columns from both classes.
这篇关于Hibernate(JPA)抽象超类的继承映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!