简体   繁体   English

使用 POJO 作为 hibernate 实体的基础

[英]Using POJO as a base for hibernate entity

I started developing in Java quite recently, and my client is also a developer who is developing in Java since it was released.我最近开始在Java开发,我的客户也是Java发布以来一直在开发的开发者。

So when he says "we have a good reason why don't we use transient fields in our project", I didn't ask what those reasons are.所以当他说“我们有充分的理由为什么不在我们的项目中使用瞬态字段”时,我没有问这些原因是什么。 But, back to the question:但是,回到问题:

I have two classes:我有两个班级:

  1. POJO, which is used solely to generate JSON: POJO,仅用于生成 JSON:
public class BaseSector implements Serializable {

    private String id;

    private String name;

    private String parentId;
  1. Entity:实体:
public class Sector {

    @Column(length = 36)
    private String id;

    @Column(length = 40)
    private String name;

    @Column(length = 36)
    private String parentId;
//  ... Bunch of other fields

Is there any way for an Entity class to extend this POJO, and add Column annotations dynamically?实体 class 有没有办法扩展这个 POJO,并动态添加 Column 注释? Or have POJO as an interface?或者有 POJO 作为接口? Or use entity class in POJO constructor?或者在 POJO 构造函数中使用实体 class?

Earlier we had something like this:早些时候我们有这样的事情:

for (Sector sector : sectors) {
    BaseSector baseSector = new BaseSector();
    baseSector.setId(sector.getId());
    baseSector.setName(sector.getName());
    baseSector.setParentId(sector.getParentId());
}

But I changed that by using BaseSector in HQL constructor... Btw, we also have SectorInfo and SimpleSectorInfo which also extend BaseSector, but that's a different subject..但是我通过在 HQL 构造函数中使用 BaseSector 改变了它……顺便说一句,我们还有 SectorInfo 和 SimpleSectorInfo,它们也扩展了 BaseSector,但这是一个不同的主题。

A TRANSIENT field tells your ENTITY class that this particular field should not be persisted in the DB.一个TRANSIENT字段告诉你的ENTITY class 这个特定的字段不应该保存在数据库中。 @Transient annotation is used to ignore a field to not persist in database in JPA, where as transient key word used to ignore a field from serialization. JPA 中@Transient注解用于忽略一个字段不持久化在数据库中,其中as transient关键字用于从序列化中忽略一个字段。 The field annotated with @Transient still can be serialized, but the field declared with transient keyword not to be persisted and not to be serialized. @Transient注解的字段仍然可以序列化,但是用transient关键字声明的字段不被持久化,不被序列化。

A POJO can be extended by an ENTITY and vice-versa. POJO 可以由 ENTITY 扩展,反之亦然。 This is stated in JPA specification.You can find more examples at the below links:这在 JPA 规范中有说明。您可以在以下链接中找到更多示例:

Link:1: JPA Non-Entity SuperClass Link:1: JPA 非实体超类

Link 2: JPA Specification 链接2:JPA 说明书

You can achieve this by using an annotation: @javax.persistence.MappedSuperclass您可以通过使用注释来实现此目的: @javax.persistence.MappedSuperclass

It states: A superclass is treated as non-entity class if no mapping related annotations such as @Entity or @MappedSuperclass are used on the class level.它声明:如果在 class 级别上没有使用与映射相关的注释(例如 @Entity 或 @MappedSuperclass),则超类将被视为非实体 class。

This means your superclass will be treated as a non-entity class here if you do not use the above annotations in your superclass.这意味着如果您不在超类中使用上述注释,您的超类将在此处被视为非实体 class。

How to Construct the classes:如何构建类:

SUPERCLASS which also a POJO for your JSON object SUPERCLASS也是你的POJO JSON object

@MappedSuperclass
public class BaseSector implements Serializable {

    private String id;
    private String name;
    private String parentId;

}

ENTITY class:实体class:

@Entity
@Table(name = "sector")
public class Sector extends BaseSector {
    @Column(length = 36)
    private String id;

    @Column(length = 40)
    private String name;

    @Column(length = 36)
    private String parentId;

    //  ... Bunch of other field

}

You can also override some property defined by BaseSector in your ENTITY - Sector You need to use您还可以覆盖实体中 BaseSector 定义的某些属性 - 您需要使用的扇区

 @AttributeOverride   // for single property
 @AttributeOverrides  // override more than one property

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

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