Correct design for entity classes. Need recommendations

我的未来我决定 提交于 2019-12-02 00:07:55

Yes, make your base entity an abstract class and let other extend it.

public abstract class BaseEntity
    {
       private long id;
       public long getId() {return this.id};
       public void setId(long id) {this.id = id;};
    }

As a general rule, one should always program to an interface and not to an implementation.

You could use an abstract BaseEntity class, mapped with @MappedSuperclass. But you still would have to override the mapping of the ID field to the appropriate column in every subclass.

Just because two classes have the same property doesn't necessarily mean that they should extend a common class. Your code will probably never reference any object with the type BaseEntity. Unless you have additional common methods, I would advise not to use a superclass in this case. It will just be simpler.

And entities are POJOs. Using an interface for every entity, in my experience, just adds unnecessary complexity.

Making BaseEntity abstract is perfectly good, I have used it myself this way. And I don't think there's anything else you can abstract. You could abstract if you would have multiple tables that all have some common columns, such as for auditing purposes. And interfaces for entities? I don't think that's anything useful. Interfacing is more useful when you have to switch different implementations, now in entities, that doesn't make much sense.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!