Brief introduction
This series contains two articles, and part 1th discusses several basic best practices for Hibernate and other object-relational mapping (ORM) tools. By using common base domain classes and interfaces, centralized auditing, and generic data Access Objects (generic DAO), applications can build a more compact and maintainable layer of domain models and persistence.
By applying the concepts in part 1th, you can provide new opportunities for code reuse. In this section, we first discuss how to use Hibernate and polymorphism to integrate behavior into the domain model. Next, proceed to the 1th part of the discussion of the generic DAO. After you integrate and use the generic DAO in your application, you may find more common operations. We'll show you how to integrate data paging and querying in a generic DAO, which reduces code. Finally, the strategy of improving the performance of domain model is discussed. Without these policies, the association of misconfigured Associations in the domain model can result in a large number of redundant queries, as well as the acquisition of unneeded records, which can be a waste of resources.
Re-discussion Model: let ORM select Behavior
Because the database tables themselves do not behave, developers often place the behavior of the domain model entity in the service or view layer. However, this approach is not appropriate because it violates the object-oriented basic rule: Object ownership behavior and data. If the behavior is transferred from the object to the service, the object becomes a pure data container. In addition, placing an entity's behavior in a service or view layer causes the core logic of the entity to be dispersed in many parts of the application, leading to maintenance problems. Tools such as Hibernate help to put behavior and data in the model, which helps to construct domain-driven models.
Let's continue with the example of the employee used in part 1th. The object model given in Figure 1 defines the pay-for-payment approach function:
Figure 1. The object model of salary payment method calculation