r/SpringBoot • • Dec 14 '22

Difference between DTO and Entity and DAO

Hi, I am learning Spring Data JPA and confused on some classes.

Could someone please explain the differences between above mentioned class types? The resource where I am learning it has not explained much about it, but I see that the class structures are similar between DTO and Entity. DTO class has a static method which returns customerentity and vice versa with entity class too.

Couldn't find any sub on spring data so posted here.

25 Upvotes

18 comments sorted by

16

u/Sheldor5 Dec 14 '22

Entity - a living object which is directly attached to a database row, any method call potentially executes SQL queries in the background (fetching child rows etc ...), any modification to the object's properties will execute UPADTE/INSERT/... SQL queries in the background, only your persistence and service layer should use them!!!!!!

DTO - plain old Java object, used to pass data between layers (http <> controller, controller <> service)

DAO - like the service layer but focusing purely on data modification/reading

1

u/[deleted] Dec 14 '22

Can you please explain the reason why we use static methods in DTO and Entity which return there vice versa objects?

4

u/Sheldor5 Dec 14 '22

never seen static mapper methods on those classes itself

but if you are working with Entities inside the service you need to map the (live) Entity to a DTO which you return to the caller (e.g. controller)

so those are simple Mappings from one Class to the other Class (e.g. MapStruct helps so you don't have to manually write the mapping code)

2

u/[deleted] Dec 14 '22

So, we have DTO to act between controller and Entity class, am I understanding it correctly?

3

u/GameComited Dec 14 '22

Lets see a simple example. if I want all the info from a row of the DB i can make a call to the controller like localhost/api/customer/1. The controller pases the id: 1 to the service ( first input layer) -> the service use the Dao to make the query with the id in the DB ( second input layer) -> the Dao returns the whole row ( the entity ) to the service -> now the service map the entity to a DTO (with an existing mapper or manually,...) And then send the DTO to the controller ( first output layer) and finally the controller send the DTO (second output layer) with all the details from the Entity and you can see It on json format on the browser

The Entity represents the row of the DB The DTO represents the java object that Will travel between layers The DAO is a Class that allows you the access to the DB (queries, inserts, delete,...)

Maybe i am wrong but thats how i understood this from the beginning Sorry for the grammar mistakes, i am not a english speaker

1

u/Fermi-4 Dec 14 '22

What is the general scope of the DAO? One per entity or table or relation? What is the benefit of using DAO in the service layer?

1

u/GameComited Dec 14 '22

Dao is in the persistence layer, It Is usually call from the service, the scope is one per table.

You normally have customerController - customerService - customerDao This customerDao extends JPARepository So from the service you can use JPA methods like customerDao.FindById( 1 ) and this is going to return the fisrt row of the customer table

Now you have the Entity in the service layer and you can map It to a DTO and send It back to the controller

1

u/GameComited Dec 14 '22

Dao is in the persistence layer, It Is usually call from the service, the scope is one per table.

You normally have customerController - customerService - customerDao This customerDao extends JPARepository So from the service you can use JPA methods like customerDao.FindById( 1 ) and this is going to return the fisrt row of the customer table

Now you have the Entity in the service layer and you can map It to a DTO and send It back to the controller

1

u/GameComited Dec 14 '22

Dao is in the persistence layer, It Is usually call from the service, the scope is one per table.

You normally have customerController - customerService - customerDao This customerDao extends JPARepository So from the service you can use JPA methods like customerDao.FindById( 1 ) and this is going to return the fisrt row of the customer table

Now you have the Entity in the service layer and you can map It to a DTO and send It back to the controller

1

u/Fermi-4 Dec 14 '22

Ok I see - I was calling it a Repository/Persistence layer but really they are the same pattern as DAO..

1

u/GameComited Dec 14 '22

I call It Repository also , maybe i am a bit wrong with the example, i just told you what i have learn by my self and what i ussually do. Google says this about Dao an Repository

DAO is an abstraction of data persistence. Repository is an abstraction of a collection of objects. DAO would be considered closer to the database, often table-centric. Repository would be considered closer to the Domain, dealing only in Aggregate Roots.

2

u/greglturnquist Dec 15 '22 edited Jun 22 '25

I made a video on this very topic…

https://youtu.be/mRHSCQ9PYso

UPDATE: this video was available two years ago when I first shared it. Since then I’ve pivoted my channel away from Spring content and essentially taken Spring videos out of general circulation (except for members).

1

u/Aljonex Jun 22 '25

Downvoting and commenting as is a pay to watch video :(

1

u/greglturnquist Jun 22 '25

Check out my update on the post.

1

u/Aljonex Jun 22 '25

Thanks :)

1

u/The00fing27 Apr 01 '26

Why would you take it out of general circulation? You're losing out on any ad revenue you could get since Reddit searches are timeless

1

u/greglturnquist Apr 01 '26

I updated its visibility just for you. 😎

1

u/ksices Dec 15 '22 edited Dec 15 '22

i think these class has only used scene different,but if your project is not definition some develop rule,you can use all class whatever you want ,you can find some article about DDD, and maybe you can learn more