gpt4 book ai didi

java - @Entity 和@Embeddable 之间的继承

转载 作者:搜寻专家 更新时间:2023-10-30 20:29:51 25 4
gpt4 key购买 nike

动机

我的应用程序根据表 foo 的内容向某些 Web 服务发出请求。我想记录这些请求(在数据库中)以及用于制作它们的 foo 行的值。不过,由于表 foo 中的数据会随时间变化,我需要记录行的确切内容(而不是它的 ID)。我正在使用 Hibernate,所以我想,我可以在表示请求的实体中嵌入 foo 行。为了拥有干净的类层次结构,我想要这样的东西:

 class Foo
{
/*
* Here goes some properties of Foo, which specify columns names etc.
*/
}

@Embeddable
class EmbeddableFoo extends Foo
{
//EmbeddableFoo should have the same properties as Foo
}

@Entity
class EntityFoo extends Foo
{
@Id
Long getId() { //EntityFoo has some id }
//EntityFoo should have the same properties as Foo, except that
//it has id
}

问题

这不会起作用,因为EmbeddableFoo 没有看到 Foo 的属性。我可以在 Foo 上添加 @Inheritance 注释,以便 EntityFoo *看到* Foo 的属性,但它会不能用 EmbeddableFoo

解决我的问题

问题

是否有一种简单的(即不需要编写大量冗余代码)方法让 @Embeddable 和 @Entity* 从同一个类继承?

最佳答案

你检查注释了吗@MappedSuperclass ?这可能就是您正在寻找的。

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

Designates a class whose mapping information is applied to the entities that inherit from it. A mapped superclass has no separate table defined for it.

A class designated with the MappedSuperclass annotation can be mapped in the same way as an entity except that the mappings will apply only to its subclasses since no table exists for the mapped superclass itself. When applied to the subclasses the inherited mappings will apply in the context of the subclass tables. Mapping information may be overridden in such subclasses by using the AttributeOverride and AssociationOverride annotations or corresponding XML elements.

关于java - @Entity 和@Embeddable 之间的继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12597806/

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