简体   繁体   English

Java持久性实体是否应该有一个公共父对象作为常规对象?

[英]Should a Java persistence entity have a common parent as the regular object?

I'm cleaning up some code, and I have a class with an entity that implement a lot of the same behaviour. 我正在清理一些代码,并且有一个带有实体的类,该实体实现了许多相同的行为。 Should I combine that behaviour into a parent that both inherit or is there a reason to keep them separate? 我应该将这种行为结合到一个既继承又父的行为中,还是有理由将它们分开?

(There are also some transfers of data between the entity and normal class that would be made easier if I could upcast to a method that would pass data between them) (如果我可以转换为在实体和普通类之间传递数据的方法,那么在实体和普通类之间也将进行一些数据传输)

Also what to do about the database specific additions to the shared variable? 另外,如何对共享变量进行数据库特定的添加?

For example, in the below code, should I create a class MyBase and extract prop1 plus its getters and setters to the MyBase class and then have MyObject and MyObjectEntity both extend MyBase ? 例如,在下面的代码中,我是否应该创建一个MyBase类并将prop1以及其getter和setter提取到MyBase类,然后让MyObjectMyObjectEntity都扩展MyBase

Normal: 正常:

public class MyObject {
    private String prop1;

    public MyObject() {}

    public String getProp1() {
       return prop1;
    }

    public void setProp1(String prop1) {
       this.prop1 = prop1;
    }

    public void doSomethingNormal() {
       //Do something normal
    }
}

Entity: 实体:

@Entity
public class MyObjectEntity
{
    @Column(unique = true, nullable = false)
    private String prop1;

    public MyObjectEntity() {}

    public String getProp1() {
       return prop1;
    }

    public void setProp1(String prop1) {
       this.prop1 = prop1;
    }
    public void doSomethingEntity() {
       //Do something entity like
    }
}

我最终无法执行此操作,因为它导致了我的旧序列化对象的问题,并决定在课堂上尽可能多地进行分解。

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

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