Is there any special handling I need to take into account when I use JPA/Hibernate with Spring and Struts with regard to the session? I seem to have no problem with my DAO objects querying the table and getting a list of results or creating objects. But the remove was throwing an exception until I changed the code from
getEntityManager().remove(entity); to
getEntityManager().remove(getEntityManager().merge(entity));. Now that seemed to resolve the issue but now find throws an exception. Here's the code:
Code:
public T findById(Object id) {
return(getEntityManager().find(clazz, id));
}
The exception stack is:
java.lang.NullPointerException
org.hibernate.impl.SessionImpl.get(SessionImpl.java:836)
org.hibernate.ejb.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:182)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:194)
$Proxy9.find(Unknown Source)
com.setech.sampleapp.hibernate.dao.GenericDAO.findById(GenericDAO.java:71)
com.setech.sampleapp.hibernate.service.AuthorService.findById(AuthorService.java:45)
com.setech.sampleapp.hibernate.service.AuthorService.findById(AuthorService.java:1)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:310)
org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
$Proxy12.findById(Unknown Source)
com.setech.sampleapp.struts2.AuthorAction.edit(AuthorAction.java:41)
Can someone explain to me what i have overlooked with respect to configuration?
applicationContext.xml
Code:
<?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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.0.xsd">
<!--
This is the BeanPostProcessor object that processes PersistenceUnit
and PersistenceContext annotations, for injection of the corresponding
JPA resources EntityManager and EntityManagerFactory. Any SPring managed
objects with these annotations will be injected.
Did not see this referenced here
http://www.springbyexample.org/examples/one-to-many-jpa-template-config.html
-->
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<!-- Import -->
<import resource="sampleappContext.xml"/>
<!--
Many references here have pointed to using
org.springframework.orm.hibernate3.LocalSessionFactoryBean
But examples located here
http://www.springbyexample.org/examples/one-to-many-jpa-template-config.html
recommend that you use the LocalContainerEntityManagerFactoryBean object
instead.
-->
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- <property name="persistenceUnitName" value="simple-jpa-template"/> -->
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="MYSQL" />
<!-- <property name="database" value="SQL_SERVER" /> -->
<property name="showSql" value="true" />
<property name="generateDdl" value="true" />
</bean>
</property>
</bean>
<!-- Local MySQL Installation -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3308/sampleapp2" />
<property name="username" value="root" />
<property name="password" value="w3bl0g1c" />
</bean>
<!--
Some examples have shown for the transactionManager to reference class
org.springframework.orm.hibernate3.HibernateTransactionManager
But it appears that JPA/Hibernate have been integrated and the following
definition is expected for this framework to work.
-->
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<!-- Defines our transaction manager for transaction annotated objects -->
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>