Best Practices — Hibernate persistence code inside the Entity itself?

走远了吗. 提交于 2019-12-22 08:26:56

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/8010764/best-practices-hibernate-persistence-code-inside-the-entity-itself

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!