Correct design for entity classes. Need recommenda

2019-02-25 01:07发布

问题:

For example, I have entity class User:

public class User
{
  private long id;
  private String name;

  // setters and getters
}

Next, I add new entity class: Comment

public class Comment
{
private long id;
private String comment;

// setters and getters
}

Next, I can add more and more entity classes.

And, at this moment I think: I can/must bind/connect in logical structure my entity classes or no?

What I mean? I try explain:

Point 1: All this classes: User, Comment and more other - POJO.

Idea 1: Need logical binding for this classes via interface or abstract class.

Point 2: I see, that All entity classes has same methods: getId and setId().

Idea 2: Need to avoid declaration this methods in all classes.

My Solution:

Add interface BaseEntity:

public interface BaseEntity
{
public long getId();
public void setId(long id);
}

Add all entity classes must implement this interface.

In result we logical connect all entity classes. And we guarante that each entity class implement getId() and setId() methods.

But this solution doesn't resolve problem with multiple declaration getId and setId.

A solution is to create general BaseEntity class:

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

And all entity class must extends BaseEntity class.

mmmm, sound nice :)

But, with current implementation - user can create instanse BaseEntityClass. This make sense? I can give possibility to create a class BaseEntity?

Or maybe, good solution mark this class as abstract? What do you think?

And if you agree with all my previous steps:

I have last question:

Communication beetween classes must based on Interfaces. But I dont have interface for entities. It is can create problems for me in future?

Thank you.

回答1:

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.



回答2:

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.



回答3:

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.