Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct design for entity classes. Need recommendations

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.

like image 738
user471011 Avatar asked Nov 26 '25 19:11

user471011


2 Answers

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.

like image 149
Ravi Bhatt Avatar answered Nov 29 '25 09:11

Ravi Bhatt


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.

like image 40
JB Nizet Avatar answered Nov 29 '25 09:11

JB Nizet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!