Data persistence in Java Server Faces with Hibernate/JPA

I am planning to build a web application with Java Server Faces but i am unsure how to handle the persistence of my entities. For php projects i am using the Symfony framework with Doctrine 2. I call the persist method of the entity manager mainly in the controllers. So there are no seperate classes for business logic. I tried Hibernate and i think that is a good replace for Doctrine. ;)

The java project is for university and i want to transfer the business logic from the controllers/beans to classes for business logic.

I read articles and tutorials about the data access object and repository pattern (http://docs.spring.io/spring-data/jpa/docs/1.3.0.RELEASE/reference/html/jpa.repositories.html).

In the data access object pattern i have a class for my Entity and an interface and a class for my DAO object/entity, in the repository pattern i have a class for my entity and a class for the repisitory.

But where goes my business logic? Do I have to write an interface and a class per entity for the business logic layer and inside the classes i simply call my dao/repository methods? If so, what is the difference between both patterns?

If I am totally wrong: What is the (industry) standard for data persistence (with Hibernate/JPA) with Java Server Faces?


The architecture of the project must be well organized according to principle "low coupling and high cohesion"

The system layers should be well seperated. The basic architecture have 3 layers.

  • Presentation Tier: JSF managed beans are in this layer. The task of this layer is to correspond user actions, hold user data and show the data to the user. Basically, its responsibility is bounded by MVC pattern regardless of which implementation you use (JSF, Spring MVC or Struts)
  • Bussimess Logic Tier: The data and actions which was collected from page was sent to this layer to operate on data. You should not manipulte the data and prepare the response inside Presentation Tier. It is the responsibility of Bussiness Logic Tier.(Spring, CDI)
  • Integration Tier: Database access should be done in this layer regardless of the library technology used (Hibernate, JPA, JDBC).
  • 在这里输入图像描述

    As you can see in the pic. your JSF managed beans should only be capable of requests from the pages(controller) and page data (model). You can have a look to this answer to understand JSF MVC. Therefore, It is better to not to connect DB inside JSF managed beans. It is not a good practice.

    The second part of your question. All technologies, Hibernate, JPA, Spring JDBC, uses JDBC driver of the related DB. JDBC Drivers is the just thing which connects to the DB. However, it is beter to select a method how to connect to DB, Hibernate, JPA or Spring JDBC.

    You can download my example application which implements this architecture basically.

    链接地址: http://www.djcxy.com/p/61424.html

    上一篇: 在ERB模板中排序YAML输出

    下一篇: 使用Hibernate / JPA的Java Server Faces中的数据持久性