2
2
.
.
2
2
.
.
1
1
E
E
n
n
t
t
i
i
t
t
y
y
O
O
b
b
j
j
e
e
c
c
t
t
(
(
D
D
O
O
)
)
I
I
n
n
f
f
o
o
[
[
R
R
]
]
[
[
R
R
]
]
Entity Object, DO, PO & POJO are synonyms. (in Spring Boot where they refer to the same thing)
DO stands for Data Object or Domain Object (Domain is subject of your business logic like selling cars)
PO stands for Persistence Object (Object that persists - it is saved, usually as Record in DB)
POJO stands for Plain Old/Ordinary Java Object (just Properties, setters and getters)
Entity (something that exists: Tree, Dog, Table, Cloud)
Class represents DB Table (inside your application)
Object/Instance represents record in DB Table (inside your application)
allows you to treat DB Record as an Object (inside your application)
Entity Class should only have (shouldn't contain any business logic or DB operations)
Properties that are equivalent to Table Columns (can be of the same name or mapped through configuration)
Optional helper Methods (setters, getters, equals, toString)
Optional Constructor (for settings all the properties at once)
Command (DTO) as equivalent purpose as Entity (DO)
Command (DTO) is used when beck-end wants to send/retrieve data to front-end
Entity (DO) is used when beck-end wants to send/retrieve data to DB
PersonEntity.java
package com.ivoronline.test_spring_boot.model;
public class PersonEntity {
//PROPERTIES
private Long id;
private String name;
private Integer age;
//CONSTRUCTORS
public PersonEntity() { }
//SETTERS
public void setId (Long id) { this.id = id; }
public void setName(String name) { this.name = name; }
public void setAge (Integer age) { this.age = age; }
//GETTERS
public Long getId () { return id; }
public String getName() { return name; }
public Integer getAge () { return age; }
}
E
E
n
n
t
t
i
i
t
t
y
y
Generally speaking Entity is something that exists.
Entity refers to anything that exists and has properties that differentiate it from other stuff.
We give different names to different Entities so that we could know what we are referring to while communicating.
Examples of Entities are: Tree, Dog, Table, Cloud.
Generally speaking Entity has set of Properties (name, color, size) and set of behaviors (run, sing, calculate).
In OOP Languages Entities are modeled using Classes which have Properties and Methods.
But inside Spring Boot
Entity has a more restricted meaning and includes only static Properties (without behaviors).
Entity Class is equivalent to DB Table.
Instance of Entity Class is equivalent to single Record in DB Table.
P
P
O
O
J
J
O
O
[
[
R
R
]
]
POJO is Plain Old/Ordinary Java Object that has following characteristics
Constructor is optional
there is no restriction on access-modifiers of fields (Fields don't have to be private)
Fields/Properties can be accessed directly (they don't need to be accessed through constructors, getters or setters)
it should not extend prespecified classes: class NotPOJO extends HttpServlet
it should not implement prespecified interfaces: class NotPOJO implements EntityBean
it should not contain prespecified annotations: @Entity class NotPOJO
J
J
a
a
v
v
a
a
B
B
e
e
a
a
n
n
[
[
R
R
]
]
Java Bean is a special type of POJO that has following restrictions
it must implement Serializable interface
it must have no-arg Constructor (Constructor that accepts no arguments)
direct access to Fields is forbidden
all Fields/Properties must be private
all Fields/Properties must have getters or setters or both
Fields/Properties can be accessed through constructors
D
D
o
o
m
m
a
a
i
i
n
n
&
&
D
D
o
o
m
m
a
a
i
i
n
n
O
O
b
b
j
j
e
e
c
c
t
t
Domain is subject/purpose of your business logic.
Domain Object is an Object/Entity that is related to that Domain.
So Domain is what the application is about and Domain Objects are Objects that will be used inside application.
For example
Domain = selling stuff & Domain Objects = car, receipt, customer
Domain = growing vegetables & Domain Objects = tomato, worker, storage
Domain = healing pet & Domain Objects = pet, owner, appointment
L
L
o
o
m
m
b
b
o
o
k
k
-
-
H
H
i
i
d
d
i
i
n
n
g
g
H
H
e
e
l
l
p
p
e
e
r
r
M
M
e
e
t
t
h
h
o
o
d
d
s
s
The only useful part of the Entity Class (from a business logic perspective) are its static Properties.
Helper Methods (like setters and getters) are just part of the plumbing/implementation so that Entity Class could fulfill its
role of store some data/properties.
Since these helper methods are the same for all Entities, and therefore do not provide any useful information, they are in
our way obfuscating the true purpose of Entity Class.
Therefore it is useful to hide helper Methods so that we can focus on different Properties as we move between Entities.
For that purpose we can use Lombok API which implements Annotations that provide such service.
Lombok's Annotations inject helper Methods behind the scene allowing us to ignore them inside Entity Class.