java - SingleTable策略,如果子类中没有指定表名,会抛出异常吗?

标签 java sql hibernate inheritance jpa

我有一个表层次结构,如下所示:

@MappedSuperclass
@Table(name = "v_contract_account", schema = "SAP")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@XmlRootElement
@XmlSeeAlso({CurrencyAccount.class, ProgramAccount.class})
@XmlAccessorType(XmlAccessType.FIELD)
public abstract class AbstractContractAccount implements Serializable {
    ....
}

@Entity
@Table(name = "v_contract_account", schema = "SAP")
@Immutable
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.INTEGER)
@DiscriminatorValue("0")
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class CurrencyAccount extends AbstractContractAccount  {
    ...
}

@Entity
@Table(name = "v_contract_account", schema = "SAP")
@Immutable
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.INTEGER)
@DiscriminatorValue("1")
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class ProgramAccount extends AbstractContractAccount {
    ...
}

现在它按原样工作(除了鉴别器的东西),但是为什么如果我从子类中删除表注释,Hibernate 会抛出异常?

org.jboss.resteasy.spi.UnhandledException: java.lang.ClassCastException: org.jboss.resteasy.specimpl.BuiltResponse cannot be cast to [Lcom.zanox.internal.billingmasterdata.domain.entity.CurrencyAccount;

奇怪的是,如果我不将表和继承注释放在抽象父类(super class)中,一切仍然正常。这是否意味着 MappedSuperClass 不关心表和继承注释?如果任何地方都不需要注释@Inheritance(strategy = InheritanceType.SINGLE_TABLE),那么我在哪里指定它?

顺便说一句,就我而言,Hibernate 不会创建表,表已经存在,我只想映射它。

最佳答案

您可能想从父实体中删除 @MappedSuperClass 注释并使其成为普通实体。

http://docs.oracle.com/javaee/5/api/javax/persistence/MappedSuperclass.html

如果您想跨 AbstractContractAccount 进行查询,那么它必须是一个实体。当它是 MappedSuperclass 时,您不能执行此操作。

当您想要定义一些通用映射但没有实际的“数据库继承”时,您可以使用@MappedSuperClass。

http://en.wikibooks.org/wiki/Java_Persistence/Inheritance

Mapped superclass inheritance allows inheritance to be used in the object model, when it does not exist in the data model. It is similar to table per class inheritance, but does not allow querying, persisting, or relationships to the superclass. Its main purpose is to allow mappings information to be inherited by its subclasses. The subclasses are responsible for defining the table, id and other information, and can modify any of the inherited mappings. A common usage of a mapped superclass is to define a common PersistentObject for your application to define common behavior and mappings such as the id and version. A mapped superclass normally should be an abstract class. A mapped superclass is not an Entity but is instead defined though the @MappedSuperclass annotation or the element.

关于java - SingleTable策略,如果子类中没有指定表名,会抛出异常吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19854898/

相关文章:

java - 如何在android RecyclerView中将onBindViewHolder与多个项目一起使用

java - 将 JSON 设置为 EditText

C# SQL 输出代码问题

java - 将playframework子模块中的jpa/hibernate @Entity添加到实体管理器中进行扫描

java - Spring @Transactional 超时未按预期工作

java - 无法让 Activity 对象在 Jira 中工作

sql - 从 SQL 递归查询中添加额外的列

mysql - mysql 中的 AES_ENCRYPT 返回 '

mysql - Spring Hibernate @Data 更新与原始 UPDATE 查询 : Optimistic Lock Exception despite @Transactional?

java - 如何从表中读取所有记录(> 1000万条记录)并将每条记录作为 block 响应?