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`? 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 } }
Totally depends. Maybe an interface is more appropriate, and a property-copy util like Commons' BeanUtils. Hard to say with the info given. Using a normal class as an entity base class can cause a different set of problems depending on persistence framework, how/if reflection is used, etc.

以上就是Should a Java persistence entity have a common parent as the regular object?的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏