Search code examples
javajacksongsonnaming

Why are POJOs intended to be serialised/deserialised to JSON commonly contained in an 'entities' package? What is the meaning of 'entities'?



Question Similarity i.e. this is not a duplicate

N.B: there are a number of questions which seem similar to this one:

Mapping POJO to Entities

What is the exact meaning of the JPA @Entity annotation?

What is an Entity? Why is it called Entity?

But they either refer to the 'Entity Framework' or the '@Entity' annotation (which is apparently used to stated that a class can be mapped to a table). Although my question may appear similar to these it is slightly different and so should not be considered a duplicate.


My actual question

I am working with a codebase which has a package called 'com.xxx.yyy.entities'. This package contains a number of classes all with essentially the same structure:

  1. private fields
  2. public getters for each private field
  3. public setters for each private field
  4. a 'toString' method.

An example of one of the simplest classes is below:

public class Competition {
  private String id;
  private String name;
  public String getId()            { return id;        }
  public void setId(String id)     { this.id = id;     }
  public String getName()          { return name;      }
  public void setName(String name) { this.name = name; }
  public String toString() { 
    return "{" + "" + "id=" + getId() + "," + "name=" + getName() + "}"; 
  }
}

The classes contained within the 'entities' package are never actually instantiated but instead are always serialised/deserialised to/from JSON (this particular project uses GSON to achieve this).

My questions are:

  1. what exactly does 'entity' mean here?
    • Does it basically just mean a class which is intended to be serialised to/from JSON? Is there not a more concrete term for this rather than the extremely generic 'entity'?
  2. Is this a common pattern within Java projects?

Solution

  • In the traditional sense, an "Entity" is an object that gets persisted to a relational DB. Typically (but not necessarily) an Entity maps to a table. In a more general sense, en Entity represents the implementation of a mapping to other serialised objects, like JSONs.

    I think the word comes from Entity–relationship (ER) models, and again, this has to do with how you represent information in a data model, typically a relational database. It's not a Java-only concept, as ER data modelling predates Java by decades.