是否可以通过扩展 POJO 来构建 JPA 实体? [英] Is it possible to build a JPA entity by extending a POJO?

查看:21
本文介绍了是否可以通过扩展 POJO 来构建 JPA 实体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下 POJO:

Lets say I have the following POJO:

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

现在是否可以将此 POJO 扩展为 JPA 实体?

Is it now possible to extend this POJO as a JPA entity?

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

我想将 POJO 与 JPA 实体分开.POJO 存在于不同的项目中,并且经常在没有持久层的情况下使用,我的项目希望将其持久化到数据库中,并且这样做没有从 POJO 映射到实体并返回的开销.

I want to keep the POJO separate from the JPA entity. 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.

我知道 JPA 实体是 POJO,但为了使用它,我必须包含一个实现 javax.persistence 的库,而其他使用相同基础对象的项目没有用于持久层.

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.

这可能吗?这是个好主意吗?

Is this possible? Is this a good idea?

推荐答案

JPA 规范说明

实体可以扩展非实体类和实体类,非实体类可以扩展实体类.

Entities may extend non-entity classes as well as entity classes, and non-entity classes may extend entity classes.

@javax.persistence.MappedSuperclass 注解允许你定义这种映射

@javax.persistence.MappedSuperclass annotation allows you to define this kind of mapping

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

    // getter's and setter's
}

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


}

如 JPA 规范所述

MappedSuperclass 注释指定一个类其映射信息应用于从它继承的实体.

The MappedSuperclass annotation designates a class whose mapping information is applied to the entities that inherit from it.

使用 MappedSuperclass 注释指定的类可以以与实体相同的方式映射,只是映射将仅应用于其子类,因为映射的超类本身不存在表.

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.

如果您需要覆盖由 MyThing 定义的某些属性,请使用@AttributeOverride(当您想覆盖单个属性时)或@AttributeOverrides(当您想覆盖多个属性时)

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)

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


}

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

}

如果你不想改变你的基类,你可以使用xml将其定义为@MappedSuperClass

If you do not want to change your base class, you can use xml to define it as a @MappedSuperClass

请注意:默认情况下,持久性提供程序将在 META-INF 目录中查找名为 orm.xml 的文件

Be aware: by default, the persistence provider will look in the META-INF directory for a file named 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>

没有别的.如果你想覆盖一个属性,使用@AttributeOverride,如上所示

Nothing else. If you want to override a property, use @AttributeOverride as shown above

这篇关于是否可以通过扩展 POJO 来构建 JPA 实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆