Community Blog Problems and Suggestions on Persistence Layer in Java Servers

Problems and Suggestions on Persistence Layer in Java Servers

In this article, we will introduce the problems and recommended solution for persistence layer code when it is written in the service class in Java servers.

In Java servers, persistence layer is also known as the repository layer. This layer is responsible for data persistence and is used by the business layer to access the cache and database.

In terms of functionality, it is fine to write the persistence layer code in the service class. This is why many users are happy to accept this coding method. But we may meet some problems in this way.

Main Problems

  1. The business layer and persistence layer are mixed, which does not comply with the three-tier architecture specifications of the SpringMVC server.
  2. Statements and primary keys are assembled in the business logic, which increases the complexity of the business logic.
  3. Third-party middleware is directly used in the business logic, which makes it difficult to replace the third-party persistence middleware.
  4. The persistence layer code of the same object is scattered in various business logics, which is contrary to the principle of object-oriented programming.
  5. If this coding method is used to write the unit test cases, the persistence layer interface functions cannot be directly tested.

The following is a recommended solution which takes the direct query of the database persistence middleware Hibernate as an example.

/** User DAO CLass */
public class UserDAO {
     /** Session factory */
    private SessionFactory sessionFactory;
    /** Get user function based on job number */
    public UserDO getUserByEmpId(String empId) {
        // Assemble HQLstatement
        String hql = "from t_user where emp_id = '" + empId + "'";
        // Perform database query
        Query query = sessionFactory.getCurrentSession().createQuery(hql);
        List<UserDO> userList = query.list();
        if (CollectionUtils.isEmpty(userList)) {
            return null;
        // Return user information
        return userList.get(0);

/** User Service Class */
public class UserService {
    /** User DAO */
    private UserDAO userDAO;

    /** Get user function based on job number */
    public UserVO getUserByEmpId(String empId) {
        // Query user based on job number
        UserDO userDO = userDAO.getUserByEmpId(empId);
        if (Objects.isNull(userDO)) {
            return null;
        // Convert and return user
        UserVO userVO = new UserVO();
        BeanUtils.copyProperties(userDO, userVO);
        return userVO;

For other chaos and solutions for Java servers, you can go to Let's Talk about Some of the Chaos You'll Find on Java Servers in Startups.

Related Blog Posts

Database Tuning Best Practices - A Success Story

SaaS service providers must always consider the large number of users when designing an appropriate business architecture. The large user base and massive user data require both efficiency and stability in the infrastructure construction. However, traditional infrastructure construction programs are costly, laborious, and involve complex implementation.

When the user completes the initialization and starts business operations, the business layer will call the operation interface of DB Wrapper. After DB Wrapper receives the request, it will match the router based on the User_id passed in from the business layer to determine the final RDS instance and database on which to operate. After making the judgment, the next task will be establishing connections and executing the requests systematically. Specific code implementation requires their combination with their respective persistence layer framework and a developer who has made some research on the persistence layer framework should be able to complete the process.

From Containers to Cloud Native - Service Mesh

This article summarizes the presentation at the Yunqi Community Director's Course and shares some practical tips and development trends of the Cloud Native technology.

Alibaba Cloud Container Service provides a solution that supports the end-to-end analysis of Jaeger based on SLS. No maintenance is required in the data persistence layer.

Related Documentation

Server integration

This topic describes the integration of the Alibaba Cloud LongVideo AppServer. You can use this AppServer for many events such as data exchange between the LongVideo application and ApsaraVideo for VOD, business logic processing, and data management. For example, you can randomly generate users, obtain video lists, obtain credential information, upload videos, and process various callback events. The technologies involved include the Spring Boot microservice framework, ApsaraDB RDS for MySQL, and MyBatis persistence framework.

The AppServer project supports the client in calling relevant methods and organizes data logic for the client. The directory structure of the project can be divided to the control layer, dao layer, service layer, and persistence layer.

The following Alibaba Cloud services are required: Elastic Compute Service (ECS) with Java environment configured, ApsaraDB for RDS, and ApsaraVideo for VOD.

HSF overview

High-speed Service Framework (HSF) is a distributed RPC service framework widely used within the Alibaba Group.

HSF connects different business systems, decoupling the implementation of the systems from each other.HSF unifies service publishing/call methods from the perspective of distributed applications, helping you conveniently and quickly develop distributed applications. It provides public function modules, which avoid complex technical details in distributed systems, such as remote communication, serialization implementation, performance loss, and synchronous/asynchronous call method implementation.

The persistent configuration center is used to store various governance rules of HSF services. At startup, the HSF client subscribes necessary service governance rules, such as routing rules, grouping rules, and weight rules, from the persistent configuration center to intervene in the address selection logic of the calling procedure based on the rules.The role of persistence configuration center is played by Diamond.

0 0 0
Share on

Alibaba Clouder

2,600 posts | 750 followers

You may also like