我有以下类(class):
AbstractEntity,它是我所有实体的父类(super class),存储所有实体使用的公共(public)字段,例如 id:
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class AbstractEntity implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
protected Long id;
...
}
AbstractUser,它是所有用户实体(admin、standard 等)的父类(super class),存储所有用户帐户共有的字段,例如登录名和密码等:
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class AbstractUser extends AbstractEntity implements Serializable
非抽象用户类 AdminUser 的示例:
@Entity
public class AdminUser extends AbstractUser implements Serializable {
我想要的是,我有一张用于 AbstractUser 类的表,其中仅包含 AbstractUser 类中的列,然后为每个子类提供一张包含所有类特定值的表。我认为 AbstractUser 中的 @Inheritance(strategy = InheritanceType.JOINED) 注释应该这样做。然而,Hibernate 为我生成了一个模式,每个类(AbstractUser 和所有扩展它的类)都有一个表,非抽象类的表包含抽象类的所有列。
当我保留扩展 AbstractUser 的类之一的对象时,所有值都写入更具体的表,即 AbstractUser 表保持为空。
澄清一下:
假设我有一个 AbstractUser 类以及扩展 AbstractUser 的 AdminUser 和 StandardUser 类。然后我保留一个 ID 为 1、名称为“A”的 AdminUser 对象和一个 ID 为 2、名称为“B”、帐号为“001”的 StandardUser,我最终会得到这样的结果:
抽象用户:
| ID | NAME |
empty table
管理员用户:
| ID | NAME |
| 1 | A |
标准用户:
| ID | NAME | AccountNum |
| 2 | B | 001 |
我想得到的是:
抽象用户:
| ID | NAME |
| 1 | A |
| 2 | B |
管理员用户:
| ForeignKey |
| 1 |
标准用户:
| ForeignKey | AccountNum |
| 2 | 001 |
我使用的注释有什么问题?我怎样才能做到这一点?
以防万一,我的 persistence.xml:
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="cfadbPU" transaction-type="JTA">
<jta-data-source>java:jboss/datasources/cfadbDS</jta-data-source>
<properties>
<property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.JBossAppServerJtaPlatform" />
<property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/cfadb"/>
<property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>
<property name="hibernate.connection.username" value="*****"/>
<property name="hibernate.connection.password" value="*****"/>
<property name="hibernate.default_schema" value="public"/>
<property name="hibernate.connection.autocommit" value="false"/>
<property name="hibernate.enable_lazy_load_no_trans" value="true"/>
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
</properties>
</persistence-unit>
</persistence>
更新:
我查看了服务器日志并看到了这些消息:
WARN [org.hibernate.cfg.AnnotationBinder] (ServerService Thread Pool -- 187) HHH000138: Mixing inheritance strategy in a entity hierarchy is not allowed, ignoring sub strategy in: entities.users.AbstractUser
WARN [org.hibernate.cfg.AnnotationBinder] (ServerService Thread Pool -- 187) HHH000137: Root entity should not hold an PrimaryKeyJoinColum(s), will be ignored
WARN [org.hibernate.cfg.AnnotationBinder] (ServerService Thread Pool -- 187) HHH000137: Root entity should not hold an PrimaryKeyJoinColum(s), will be ignored
Mixing inheritance strategy 是否存在错误,因为我首先为 AbstractEntity 使用 TABLE_PER_CLASS,然后为 AbstractUser 使用 JOINED?如果这是错误的原因,为什么不允许混合继承类型?我不明白这怎么会导致任何问题??
最佳答案
AbstractEntity 应该是一个 MappedSuperclass 而不是一个实体。
http://en.m.wikibooks.org/wiki/Java_Persistence/Inheritance#Mapped_Superclasses
Mapped superclass inheritance allows inheritance to be used in the object model, when it does not exist in the data model.
@MappedSuperclass
public abstract class AbstractEntity implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
protected Long id;
...
}
关于java - Hibernate JPA 使用 InheritanceType.JOINED 为每个类创建一个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29243829/