0

I currently have two problems, but first ill explain the situation:

I have a Struts2 Hibernate project, then i created a interceptor stack with the classname HibernateSessionInterceptor. I call that interceptor stack everytime the user calls a action.

Now my problems:

  • The first time i startup my web application it tries to recreate all the tables, while the tables are already created in the database.
  • The second problem is that after my web application tries to recreate all the tables, the action does not go to my controller, and it keeps saying transaction already active.

My code:

HibernateSessionInterceptor:

public class HibernateSessionInterceptor extends AbstractInterceptor {

    /**
     * 
     */
    private static final long serialVersionUID = -4986134131287730428L;

    /**
     * @see com.opensymphony.xwork2.interceptor.AbstractInterceptor#init()
     */
    @Override
    public void init() {
        DAOFactory.setTheFactory(DAOFactories.HIBERNATE.getTheFactory());
        super.init();
    }

    /**
     * @see com.opensymphony.xwork2.interceptor.AbstractInterceptor#destroy()
     */
    @Override
    public void destroy() {
        DAOFactory.setTheFactory(null);
        super.destroy();
    }

    /**
     * @see com.opensymphony.xwork2.interceptor.AbstractInterceptor#intercept(com.opensymphony.xwork2.ActionInvocation)
     */
    @Override
    public String intercept(ActionInvocation invocation) throws Exception {
//      if(HibernateSessionManager.getSessionFactory().getCurrentSession().isOpen()) {
//          HibernateSessionManager.getSessionFactory().getCurrentSession().close();
//      }
        HibernateSessionManager.getSessionFactory().getCurrentSession().beginTransaction();
        
        return Action.SUCCESS;
    }

}

CourseController:

public class CourseController {
    private static final long serialVersionUID = 1L;
    
    public String courseCode;
    public String schoolName;
    public String schoolNameWithoutArticle;
    public String coursePrice;
    public String secondCoursePrice;
    public String email;
    public int maximumRegistrations;
    public int currentRegistrations;

    public String execute() throws Exception {
        System.out.println(schoolName);
        DAOFactory.getTheFactory().getCourseDAO().findById("ADNM10005").delete();
        return "success";
    }
// NOT INCLUDED GETTERS AND SETTERS
}

struts.xml (resources folder):

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">

<struts>


    <constant name="struts.devMode" value="true" />
    <!-- <constant name="struts.enable.DynamicMethodInvocation" value="true" 
        /> -->

    <constant name="struts.multipart.maxSize" value="30000000" />
    <!-- <constant name="struts.action.extension" value=",,"></constant> -->

    <package name="houseoftyping" extends="struts-default">

        <!-- strict-method-invocation="true"> -->


        <!--*******************************************************************Interceptors********************************************************* -->
        <interceptors>
            <interceptor name="HibernateSessionInterceptor"
                class="com.houseoftyping.controller.interceptor.HibernateSessionInterceptor"></interceptor>
            <interceptor-stack name="HibernateStack">
                <interceptor-ref name="HibernateSessionInterceptor"></interceptor-ref>
                <interceptor-ref name="defaultStack"></interceptor-ref>
            </interceptor-stack>
        </interceptors>


        <!-- **********************************************************************Actions********************************************************* -->

        <default-action-ref name="UnderConstruction"></default-action-ref>

        <action name="index">
            <result>/index.jsp</result>
        </action>

    </package>
    <!-- ********************** Remove comments of your xml file if it is without 
        errors. ************************ -->
    <!-- ********************** Add new xml-files if needed. **************************************************** -->


    <include file="struts/struts-dashboard.xml"></include>

</struts>  

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
    http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
    version="4.0">
    <display-name>HouseofTyping</display-name>

    <session-config>
        <session-timeout>15</session-timeout>
    </session-config>

    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
        <init-param>
            <param-name>struts.action.extension</param-name>
            <param-value>,</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>FORWARD</dispatcher>
        <dispatcher>INCLUDE</dispatcher>
    </filter-mapping>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

    <error-page>
        <error-code>401</error-code>
        <location>/view/errorpages/ErrorPage401.jsp</location>
    </error-page>
    <error-page>
        <error-code>403</error-code>
        <location>/view/errorpages/ErrorPage403.jsp</location>
</error-page>
</web-app>

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com</groupId>
    <artifactId>houseoftyping</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>HouseofTyping website</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <jaxrs.version>2.0.1</jaxrs.version>
    </properties>

    <dependencies>
        <!-- Maven compiler plugim -->
        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.3</version>
            <type>maven-plugin</type>
        </dependency>
        
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.3.1</version>
        </dependency>
        <!-- JAX-RS -->
        <dependency>
            <groupId>javax.ws.rs</groupId>
            <artifactId>javax.ws.rs-api</artifactId>
            <version>${jaxrs.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-core</artifactId>
            <version>2.5.20</version>
        </dependency>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-convention-plugin</artifactId>
            <version>2.5.20</version>
        </dependency>

        <!-- Hibernate -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.4.1.Final</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.9.0</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.13</version>
        </dependency>

        <!-- Java Mail -->
        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>javax.mail-api</artifactId>
            <version>1.6.2</version>
        </dependency>
        <dependency>
            <groupId>com.sun.mail</groupId>
            <artifactId>javax.mail</artifactId>
            <version>1.6.2</version>
        </dependency>

        <!-- Mollie -->
        <dependency>
            <groupId>be.feelio</groupId>
            <artifactId>mollie</artifactId>
            <version>2.2.5</version>
            <exclusions>
                <exclusion>
                    <groupId>commons-codec</groupId>
                    <artifactId>commons-codec</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-api</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!-- Excel dependency -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.17</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.17</version>
        </dependency>

    </dependencies>
    <build>
        <finalName>House of Typing</finalName>
        <pluginManagement><!-- lock down plugins versions to avoid using Maven 
                defaults (may be moved to parent pom) -->
            <plugins>
                <plugin>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>3.1.0</version>
                </plugin>
                <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.22.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>3.2.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-install-plugin</artifactId>
                    <version>2.5.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>2.8.2</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

Roman C
  • 47,329
  • 33
  • 60
  • 147
LorenzoOtto
  • 112
  • 7
  • Why do you write the hibernate interceptor by hands? One of the possible solution to integrate Struts2 with Hibernate is to use the hibernate plugin. You can find more details in my answer [here](https://stackoverflow.com/a/23240899/573032). If you have a problem with hibernate creating your tables then you should provide hibernate config at least to see what properties are used. – Roman C Jan 29 '21 at 18:53
  • @RomanC can i declare the struts2 hibernate plugin that you suggest with a dependency? I am using maven so it would be easy if i could implement it with my pom – LorenzoOtto Feb 02 '21 at 15:26
  • The hibernate plugin is not available in the Maven central repository. You should install it manually like in example link provided by [this](https://stackoverflow.com/a/32376934/573032) answer. – Roman C Feb 02 '21 at 21:14

0 Answers0