Design a Java POJO with lazy loading property

前端 未结 2 1905
悲&欢浪女
悲&欢浪女 2021-01-21 01:58

Please consider below example:

A web application creates a user object for every logged in user. This object has simple String properties for firstNam

2条回答
  •  再見小時候
    2021-01-21 02:33

    I have been argued that in this way my User object is not a simple pojo

    To anwer your question I would first like to go back a bit in history.

    Pojo is a plain old java object and means that you only use "standard" java. The term was created at a time when J2EE had it's hype. At this time developers coded business logic in enterprise beans and this EJBs needed a lot of infrastructure code. This fact coupled buisness logic to an implementation technology. So Rebecca Parsons, Josh MacKenzie and Martin Fowler came to the conclusion that business logic would be more reuseable and easier to test if you just use standard java. Thus they created the term pojo, because developers like fancy names.

    Your User class just depends on standard java and therefore it is a pojo.

    Some developers argue that a pojo should not contain any logic. These developer prefer anemic models. Others say that a rich model is the better approach. I belong to the developers who prefer a rich model over an anemic model.

    If you want to remove the CarServices dependency from the User class you can implement a Car lazy loading proxy just like hibernate or a jpa implementation does.

    At least here are some of my thoughts to beans, pojos, anemic and rich domain models.

    • The difference between pojos and java beans
    • Anemic vs. Rich Domain Models

    Hopefully it helps you when you discuss wih other developers.

提交回复
热议问题