[英]Persisting non-entity class that extends an entity (jpa) - example?
The JPA tutorial states that one can have a non-entity that extends entity class: JPA教程指出,可以有一个扩展实体类的非实体:
Entities may extend both entity and non-entity classes, and non-entity classes may extend entity classes. 实体可以扩展实体类和非实体类,非实体类可以扩展实体类。 - http://java.sun.com/javaee/5/docs/tutorial/doc/bnbqa.html -http://java.sun.com/javaee/5/docs/tutorial/doc/bnbqa.html
Is it possible to persist such structure? 是否可以坚持这样的结构?
I want to do this: 我想做这个:
@Entity
abstract class Test { ... }
class FirstConcreteTest extends Test { ... } // Non-ntity
class SecondConcreteTest extends Test { ... } // Non-entity
Test test = new FirstConcreteTest();
em.persist(test);
What I would like it to do is to persist all fields mapped on abstract Test to a common database table for all concrete classes (first and second), leaving all fields of first and second test class unpersisted (these can contain stuff like EJBs, jdbc pools, etc). 我想要做的是将所有抽象类上映射到所有具体类(第一和第二个类)的公共数据库表的字段持久化,而第一和第二个测试类的所有字段都保持不变(这些类可以包含EJB,jdbc之类的东西)池等)。
And a bonus question. 还有一个奖金问题。 Is it possible to persist abstract property too? 是否也可以保留抽象属性?
@Entity
abstract class Test {
@Column
@Access(AccessType.PROPERTY)
abstract public String getName();
}
class SecondConcreteTest extends Test {
public String getName() {
return "Second Concrete Test";
}
}
Read that link again. 再次阅读该链接。 "An entity class must follow these requirements: * The class must be annotated with the javax.persistence.Entity annotation." “实体类必须遵循以下要求:*该类必须使用javax.persistence.Entity注释进行注释。”
If a class is a non-Entity, then it is a non-Entity so is not persisted as an Entity. 如果类是非实体,则它是非实体,因此不会作为实体持久存在。 Anything persistable has to be marked as such, so mark your subclass as an Entity, and mark fields that you don't want to persist as "transient" 任何可持久性都必须这样标记,因此将您的子类标记为Entity,并将您不想持久化的字段标记为“瞬态”
@Entity
(see Abstract Entities and/or Requirements for Entity Classes ). 如果要使具体类具有持久性,则必须将它们标记为@Entity
(请参阅实体类的 抽象实体和/或要求 )。 @Transient
(see Persistent Fields and/or Persistent Properties ). 如果您不希望持久化具体类的属性,则将其全部标记为@Transient
(请参阅持久性字段和/或持久性属性 )。 @Transient
. 奖励问题:我不明白为什么只要您不将它们标记为@Transient
它们为什么就不会。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.