gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 13:52:38 25 4
gpt4 key购买 nike

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

@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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com