Data Transfer Objects, Entities and Repositories
When it comes to writing a back end in Java and using a database as well, we come across a lot of new terminology. What I hope to do here is explain some of that terminology in as simple a way as possible. We would be focusing on three things in particular, DTO’s, Entitites and Repositories. So let’s jump right in!
- Data Transfer Object: A DTO is a simple class, and while writing web services, these classes are often called POJO classes, where POJO stands for Plain Old Java Object. A DTO is the object that would get passed from the client, and is also the object that is passed between the controller and the service layer in the web application. Here is an example of a simple DTO for a Student class, containing a name and an id (that would be generated automatically) :
class StudentDTO { private Integer id;
private String name;
//getters and setter, omitted for brevity}
2. Entity: An entity is what gets stored in a database. It is a POJO class, with the annotation @Entity. It also has the table name in the database mentioned with the help of an annotation @Table (name = “students_table”). Also, each attribute can be optionally given a column name using the @Column annotation. Here is an example for an entity for the Student class for which we created a DTO.
@Entity
@Table(name = "students_table")
class Student {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "student_id")
private Integer id; @Column(name = "student_name")
private String studentName; // Getters and Setters, omitted for brevity}
So why exactly do we need a DTO and an Entity for the same Student class?
An Entity is defined only for the purpose of storing it in a database. It is, simply, a Java representation of a table in a database. A DTO, on the other hand, is the class we use for all operations other than database-related ones.
Conversions between DTO’s and Entities can be done attribute-by-attribute or can be done using the copyProperties() method present in the BeanUtils class.
3. Repository: A repository is the class that interacts with the database. It performs operations on the database, like creating, updating, deleting entries, etc.
One key point to note is that an Entity is what gets stored in a database. A repository is what interacts with a database (there’s a difference).
As long as we need only simple operations (such as CRUD), we need not even write the queries for these, in case we’re using JPA (Java Persistence API’s). All we need to do to create a repository is create an interface that extends the CrudRepository, and we need not even write any method declarations in it. We just need to add a @Repository annotation to the repository interface.
Here is a simple Repository interface for the Student class.
@Repository
public interface StudentRepository extends CrudRepository<Student, Integer> {}
The repository need not contain any method declarations at all, and we can still use studentRepository.save(student) where studentRepository is an instance of StudentRepository and student is an instance of the Student (Entity) class.
In case you have heard of the term Data Access Object (DAO) it is very similar to repository and performs almost the same functions.
Hope this article clarified some of the terminology related to Data and Database interactions in Spring Boot. Don’t forget to give this article a clap 👏 if it was helpful, and follow me for more stories on a variety of frameworks!