Sunday, April 14, 2013

Struts2 + Hibernate + Jpa 2 + Spring + Netbeans how to make it all work together

After many days of frustration, searching, trying ..and failing finally got it working and because I couldn't locate any full descriptions online I will lay out my solution here.

Bare in mind that if you use different versions of these libraries, you should be sure that they are compatible with each other, because one of the issues I was dealing with (and I didn't know that at the time) was because of incompatible libraries.

So I am using:

netbeans  7.3

hibernate: hibernate-release-4.1.8.Final download
                  (+ hibernate-entitymanager-4.0.1.Final) download


listing:
hibernate-release-4.1.8.Final/lib/required/antlr-2.7.7.jar
hibernate-release-4.1.8.Final/lib/required/dom4j-1.6.1.jar
hibernate-release-4.1.8.Final/lib/required/hibernate-commons-annotations-4.0.1.Final.jar
hibernate-release-4.1.8.Final/lib/required/hibernate-core-4.1.8.Final.jar
hibernate-release-4.1.8.Final/lib/required/hibernate-jpa-2.0-api-1.0.1.Final.jar
hibernate-release-4.1.8.Final/lib/required/javassist-3.15.0-GA.jar
hibernate-release-4.1.8.Final/lib/required/jboss-logging-3.1.0.GA.jar
hibernate-release-4.1.8.Final/lib/required/jboss-transaction-api_1.1_spec-1.0.0.Final.jar
hibernate-entitymanager-4.0.1.Final


aopalliance: aopaliance 1.0  download 

aopalliance.jar

Spring    : spring3.1.1release (bundled with netbeans  7.3)


listing: 

spring-aop-3.1.1.RELEASE.jar
spring-asm-3.1.1.RELEASE.jar
spring-aspects-3.1.1.RELEASE.jar
spring-beans-3.1.1.RELEASE.jar
spring-context-3.1.1.RELEASE.jar
spring-context-support-3.1.1.RELEASE.jar
spring-core-3.1.1.RELEASE.jar
spring-expression-3.1.1.RELEASE.jar
spring-instrument-3.1.1.RELEASE.jar
spring-instrument-tomcat-3.1.1.RELEASE.jar
spring-jdbc-3.1.1.RELEASE.jar
spring-jms-3.1.1.RELEASE.jar
spring-orm-3.1.1.RELEASE.jar
spring-oxm-3.1.1.RELEASE.jar
spring-struts-3.1.1.RELEASE.jar
spring-test-3.1.1.RELEASE.jar
spring-tx-3.1.1.RELEASE.jar
spring-web-3.1.1.RELEASE.jar
spring-webmvc-3.1.1.RELEASE.jar
spring-webmvc-portlet-3.1.1.RELEASE.jar



Struts2    : struts-2.3.12 download
listing:
(remember to remove unnecessary plugins - otherwise you will have problems deploying e.g I am using tiles, so I have left the plugin in listing but I also have the corresponding  tiles.xml to make it work, if you don't just do not include struts2-tiles-plugin)

asm-3.3.jar
asm-commons-3.3.jar
asm-tree-3.3.jar
classworlds-1.1.jar
commons-beanutils-1.8.0.jar
commons-chain-1.2.jar
commons-collections-3.1.jar
commons-digester-2.0.jar
commons-fileupload-1.2.2.jar
commons-io-2.0.1.jar
commons-lang-2.4.jar
commons-lang3-3.1.jar
commons-logging-1.1.1.jar
commons-logging-api-1.1.jar
commons-validator-1.3.1.jar
ezmorph-1.0.6.jar
freemarker-2.3.19.jar
google-collections-1.0.jar
google-gxp-0.2.4-beta.jar
jackson-core-asl-1.9.2.jar
jackson-mapper-asl-1.9.2.jar
javassist-3.11.0.GA.jar
json-lib-2.3-jdk15.jar
juli-6.0.18.jar
ognl-3.0.6.jar
org.apache.felix.bundlerepository-1.2.1.jar
org.apache.felix.framework-1.4.1.jar
org.apache.felix.main-1.4.1.jar
org.apache.felix.shell-1.0.2.jar
org.apache.felix.shell.tui-1.0.2.jar
oval-1.31.jar
plexus-container-default-1.0-alpha-10.jar
plexus-utils-1.2.jar
struts2-convention-plugin-2.3.12.jar
struts2-core-2.3.12.jar
struts2-dojo-plugin-2.3.12.jar
struts2-embeddedjsp-plugin-2.3.12.jar
struts2-jasperreports-plugin-2.3.12.jar
struts2-javatemplates-plugin-2.3.12.jar
struts2-spring-plugin-2.3.12.jar
struts2-tiles-plugin-2.3.12.jar
testng-5.1-jdk15.jar
tiles-api-2.0.6.jar
tiles-core-2.0.6.jar
tiles-jsp-2.0.6.jar
xmlpull-1.1.3.1.jar
xpp3_min-1.1.4c.jar
xstream-1.4.2.jar
xwork-core-2.3.12.jar




web.xml
...
 <filter>
        <filter-name>SpringOpenEntityManagerInViewFilter</filter-name>
        <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>SpringOpenEntityManagerInViewFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

  <!-- deprecated
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    </filter>
    -->
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
        <init-param>
            <param-name>actionPackages</param-name>
            <param-value>my.action</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

...

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xmlns:aop="http://www.springframework.org/schema/aop"
               xmlns:tx="http://www.springframework.org/schema/tx"
               xmlns:context="http://www.springframework.org/schema/context"
               xmlns:p="http://www.springframework.org/schema/p" 
               xmlns:cache="http://www.springframework.org/schema/cache" 
               xmlns:jee="http://www.springframework.org/schema/jee"
               xsi:schemaLocation="
                    http://www.springframework.org/schema/beans
                    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                    http://www.springframework.org/schema/tx
                    http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
                    http://www.springframework.org/schema/aop
                    http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
                    http://www.springframework.org/schema/context     
                    http://www.springframework.org/schema/context/spring-context-3.1.xsd
                    http://www.springframework.org/schema/cache
                    http://www.springframework.org/schema/cache/spring-cache-3.1.xsd
                    http://www.springframework.org/schema/jee
                    classpath:org/springframework/ejb/config/spring-jee-3.1.xsd"> 
     
      <context:component-scan base-package="myBasePackage" />

    <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="java:comp/env/DATABASENAME"/>
    </bean>
   
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
                   <property name="dataSource" ref="dataSource" />
        <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="showSql" value="true" />
            <property name="database" value="ORACLE"/>
            <property name="databasePlatform" value="org.hibernate.dialect.Oracle10gDialect"/>
        </bean>
    </property>
      </bean>

      <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 
           <property name="entityManagerFactory" ref="entityManagerFactory" /> 
      </bean> 
      <!--mybeans -->
      <bean id="daoExample" class="my.dao.DaoExample"/>
      <bean id="portfolioService" class="my.utilities.ServiceTest"/>
      <tx:annotation-driven transaction-manager="transactionManager"/>
     
</beans>



3 comments:

  1. do you work with persistence.xml ?

    ReplyDelete
    Replies
    1. Hi! Actually I don't. After your question I searched for it in my project ..and found one ("persistence.xml") in "WEB-INF\classes\META-INF\" directory, but it is "empty" ..meaning it contains only basic structure.. I'll post its contents below:

      <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
      <persistence-unit name="struts2">
      <properties>

      </properties>
      </persistence-unit>
      </persistence>

      Delete