Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best Practices -- Hibernate persistence code inside the Entity itself?

In Google's RequestFactory tutorials, they recommend putting my persistence logic (in my case, Hibernate) inside the Entity classes. Then they pose the question: "What if you don't want to implement persistence code in an entity itself?" and proceed to explain an alternate method.

My question: which is better, putting persistence logic inside the Entity class, or keeping all persistence logic in a separate class?

Any info is appreciated, thanks.

-tjw

like image 277
Travis Webb Avatar asked Jan 18 '23 08:01

Travis Webb


1 Answers

Putting the persistence code in an entity itself is an Active Record Pattern approach while keeping all persistence logic in a separate class is the Repository Pattern approach. You can use the keyword repository pattern vs activerecord to search more information about your questions.

For example ,active record pattern has a criticism about its testability without a database , you can refer to this for more info .

For me , I prefer the Repository Pattern more as it is testable and I don't like persistence codes and domian business logic are mixed in one class which violates the emphasis on separation of concerns.

like image 53
Ken Chan Avatar answered Jan 27 '23 11:01

Ken Chan