简体   繁体   English

持续扩展实体(jpa)的非实体类-示例?

[英]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,并将您不想持久化的字段标记为“瞬态”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM