简体   繁体   English

是否可以通过扩展POJO来构建JPA实体?

[英]Is it possible to build a JPA entity by extending a POJO?

Lets say I have the following POJO: 可以说我有以下POJO:

public class MyThing {
 private int myNumber;
 private String myData;
//assume getter/setter methods
}

Is it now possible to extend this POJO as a JPA entity? 现在可以将此POJO扩展为JPA实体吗?

@Entity
@Table(name = "my_thing")
public class MyThingEntity extends MyThing implements Serializable {
 @Column(name = "my_number")
 //?????????
 @Column(name = "my_data")
 //????????
}

I want to keep the POJO separate from the JPA entity. 我想让POJO与JPA实体分开。 The POJO lives in a different project and is often used without a persistence layer, my project wants to persist it in a database and do so without the overhead of mapping from a POJO to an entity and back. POJO生活在一个不同的项目中,并且通常在没有持久层的情况下使用,我的项目希望将其保存在数据库中,并且不需要从POJO映射到实体并返回的开销。

I understand that JPA entities are POJOs, but in order to use it I would have to include a library that implements javax.persistence and the other projects using the same base object have no use for a persistence layer. 我知道JPA实体是POJO,但是为了使用它,我必须包含一个实现javax.persistence的库,而使用相同基础对象的其他项目对于持久层没有用处。

Is this possible? 这可能吗? Is this a good idea? 这是一个好主意吗?

JPA specification states JPA规范说明

Entities may extend non-entity classes as well as entity classes , and non-entity classes may extend entity classes. 实体可以扩展非实体类以及实体类 ,非实体类可以扩展实体类。

@javax.persistence.MappedSuperclass annotation allows you to define this kind of mapping @ javax.persistence.MappedSuperclass注释允许您定义这种映射

@MappedSuperclass
public class MyThing implements Serializable {
    private int myNumber;
    private String myData;

    // getter's and setter's
}

And

@Entity
@Table(name="MY_THING")
public class MyThingEntity extends MyThing {


}

As said by JPA specification 正如JPA规范所述

The MappedSuperclass annotation designates a class whose mapping information is applied to the entities that inherit from it . MappedSuperclass注释指定一个类, 其映射信息应用于从其继承的实体

And

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. 使用MappedSuperclass注释指定的类可以以与实体相同的方式映射,除了映射将仅应用于其子类,因为映射的超类本身不存在表。

If you need to override some property defined by MyThing, use @AttributeOverride (when you want to override a single property) or @AttributeOverrides (when you want to override more than one property) 如果需要覆盖MyThing定义的某些属性,请使用@AttributeOverride(当您要覆盖单个属性时)或@AttributeOverrides(当您要覆盖多个属性时)

@Entity
@Table(name="MY_THING")
@AttributeOverride(name="myData", column=@Column(name="MY_DATA"))
public class MyThingEntity extends MyThing {


}

And

@Entity
@Table(name="MY_OTHER_THING")
@AttributeOverrides({
    @AttributeOverride(name="myData1", column=@Column(name="MY_DATA_1")),
    @AttributeOverride(name="myData2", column=@Column(name="MY_DATA_2"))
})
public class MyOtherThingEntity extends MyThing {

}

If you do not want to change your base class, you can use xml to define it as a @MappedSuperClass 如果您不想更改基类,可以使用xml将其定义为@MappedSuperClass

Be aware: by default, the persistence provider will look in the META-INF directory for a file named orm.xml 请注意:默认情况下,持久性提供程序将在META-INF目录中查找名为orm.xml的文件

<?xml version="1.0" encoding="UTF-8"?>

<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_1_0.xsd" version="1.0">
    <mapped-superclass class="MyThing">

    </mapped-superclass>
</entity-mappings>

Nothing else . 没有别的 If you want to override a property, use @AttributeOverride as shown above 如果要覆盖属性,请使用@AttributeOverride,如上所示

It is possible: 有可能的:

  • you can map it with XML - make an orm.xml (conforming to the orm schema ), and map the columns of your POJO, without even extending it. 您可以使用XML映射它 - 创建一个orm.xml (符合orm架构 ),并映射POJO的列,甚至不扩展它。 It will be JPA-enabled in one environment, and a POJO in the other one 它将在一个环境中启用JPA,在另一个环境中启用POJO
  • override just the getter methods and annotate them - (I'm not sure if this will work) 只覆盖getter方法并注释它们 - (我不确定这是否可行)

That said, I don't think it is necessary to do this. 也就是说,我认为没有必要这样做。 Just annotate your POJO, and add the compile-time dependency to your projects. 只需注释您的POJO,并将编译时依赖项添加到您的项目中。 Then each project will decide whether it will use them as JPA entities or not. 然后每个项目将决定是否将它们用作JPA实体。

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

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