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

查看:139
本文介绍了是否可以通过扩展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注释指定一个类,其映射信息应用于从其继承的实体


指定的类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天全站免登陆