0

How to connect Struts 2 with hibernate and PostgreSQL?

<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost/jvmhubtutorial</property>
<property name="hibernate.connection.username">user</property>
<property name="hibernate.connection.password">password</property>
Roman C
  • 47,329
  • 33
  • 60
  • 147
Bekzod Buranov
  • 49
  • 1
  • 10

1 Answers1

1

You can integrate Hibernate and Struts2 via servlet context where you can share the session factory. The session factory is used to open Hibernate session and use it to perform queries to the database. Here an example of such integration.

In Struts2, there are no official plugins to integrate the Hibernate framework. But, you can workaround with the following steps :

  1. Register a custom ServletContextListener.
  2. In the ServletContextListener class, initialize the Hibernate session and store it into the servlet context.
  3. In action class, get the Hibernate session from the servlet context, and perform the Hibernate task as normal.

In Struts2 there's unofficial plugin called Struts2 Full Hibernate Plugin or that provides an integration with Hibernate. There're examples:

Roman C
  • 47,329
  • 33
  • 60
  • 147