Hi guys,
I'm having some problems getting Wicket/Spring/Hibernate working together. I'm following this guide:
http://wicketinaction.com/2009/06/wicke ... iguration/Things seem to be working for the most part, however I get an error when I try and invoke any transactional behaviour.
For example, if I log in to my Wicket Application the following method is run:
Code:
public boolean authenticate(final String username, final String password){
currentUser = username;
return userService.authenticate(username, password);
}
The userService seems to be DI fine as there is no NPE. However the proxy the wraps the service isn't starting the transaction as it should be.
Here is my 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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
"
default-autowire="byType"
default-lazy-init="false"
>
<bean id="wicketApplication" class="uk.co.company.product.presentation.wicket.app.WicketApplication" />
<bean id="placeholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" autowire="byName">
<property name="ignoreUnresolvablePlaceholders" value="false" />
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<property name="ignoreResourceNotFound" value="false" />
<property name="locations"><list>
<value>classpath*:/application.properties</value>
</list></property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" autowire="byName">
<property name="driverClassName"><value>${jdbc.driver}</value></property>
<property name="url"><value>${jdbc.url}</value></property>
<property name="username"><value>${jdbc.username}</value></property>
<property name="password"><value>${jdbc.password}</value></property>
</bean>
<tx:annotation-driven transaction-manager="txManager" />
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" autowire="byName">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!--<bean id="interceptor" class="com.mycompany.hibernate.HibernateInterceptor">
</bean>-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" autowire="byName">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<!-- GENERAL -->
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.show_sql">hibernate.format_sql</prop>
<prop key="hibernate.default_schema">zen_template</prop>
<prop key="hibernate.max_fetch_depth">2</prop>
<!-- <prop key="hibernate.default_batch_fetch_size">16</prop> -->
<prop key="hibernate.default_entity_mode">pojo</prop>
<prop key="hibernate.order_updates">true</prop>
<prop key="hibernate.generate_statistics">true</prop>
<prop key="hibernate.use_identifier_rollback">true</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
<prop key="hibernate.use_outer_join">true</prop>
<!-- JDBC -->
<!-- <prop key="hibernate.jdbc.batch_size">20</prop> -->
<!-- <prop key="hibernate.jdbc.batch_versioned_data">true</prop> -->
<prop key="hibernate.jdbc.use_streams_for_binary">true</prop>
<prop key="hibernate.jdbc.use_get_generated_keys">true</prop>
<!-- CONNECTION -->
<prop key="hibernate.connection.isolation">8</prop><!-- TRANSACTION_SERIALIZABLE -->
<prop key="hibernate.connection.autocommit">false</prop>
<prop key="hibernate.connection.pool_size">5</prop>
<!-- CACHE -->
<!-- <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop> -->
<!-- <prop key="hibernate.cache.use_minimal_puts">true</prop> -->
<!-- <prop key="hibernate.cache.use_query_cache">true</prop> -->
<!-- MISCELLANEOUS -->
<prop key="hibernate.bytecode.use_reflection_optimizer">true</prop>
<prop key="hibernate.cglib.use_reflection_optimizer">true</prop>
<!-- <prop key="hibernate.bytecode.provider">cglib</prop> -->
<!-- <prop key="hibernate.query.substitutions">true</prop> -->
</props>
</property>
<!--<property name="entityInterceptor">
<ref bean="interceptor" />
</property>-->
<property name="packagesToScan"><list>
<value>uk.co.company.product.persistance.hibernate</value>
</list></property>
</bean>
<context:component-scan base-package="uk.co.company.product" />
<context:annotation-config/>
</beans>
web.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd
"
version="2.4"
>
<display-name>ZenTemplate</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>opensessioninview</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter>
<filter-name>wicket-spring-hibernate</filter-name>
<filter-class>org.apache.wicket.protocol.http.WicketFilter</filter-class>
<init-param>
<param-name>applicationFactoryClassName</param-name>
<param-value>org.apache.wicket.spring.SpringWebApplicationFactory</param-value>
</init-param>
<init-param>
<param-name>applicationClassName</param-name>
<param-value>uk.co.company.product.presentation.wicket.app.WicketApplication</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>opensessioninview</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>wicket-spring-hibernate</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
CustomAuthenticatedWebSession.java
Code:
public class CustomAuthenticatedWebSession extends AuthenticatedWebSession{
private static final long serialVersionUID = 4713195500103052768L;
@SpringBean
private UserService userService;
public void setUserService(final UserService userService){
this.userService = userService;
}
transient private String currentUser = null;
public CustomAuthenticatedWebSession(final Request request){
super(request);
InjectorHolder.getInjector().inject(this);//<-- VERY IMPORTANT to get Spring DI to work (within Wicket objects)
}
@Override
public boolean authenticate(final String username, final String password){
currentUser = username;
return userService.authenticate(username, password);
}
@Override
public Roles getRoles(){
return userService.getRoles(currentUser, isSignedIn());
}
}
UserServiceImpl.java:
Code:
@Service
public class UserServiceImpl extends _AbstractService implements UserService{
final private TestUserService testUserService = new TestUserService();
@Autowired
private Temp1DAO temp1DAO;
@Override
@Required
final public void setTemp1DAO(final Temp1DAO temp1dao){
temp1DAO = temp1dao;
}
public UserServiceImpl(){
super();
//InjectorHolder.getInjector().inject(this);//<-- VERY IMPORTANT to get Spring DI to work.
}
/* (non-Javadoc)
* @see uk.co.company.product.business.spring.UserService#authenticate(java.lang.String, java.lang.String)
*/
@Override
@Transactional
final public boolean authenticate(final String userName, final String password){
final Temp1 temp1 = new Temp1();
temp1.setValue(userName);
temp1DAO.save(temp1);
return testUserService.authenticate(userName, password);
}
/* (non-Javadoc)
* @see uk.co.company.product.business.spring.UserService#getRoles(java.lang.String, boolean)
*/
@Override
@Transactional
final public Roles getRoles(final String userName, final boolean signedIn){
return testUserService.getRoles(userName, signedIn);
}
}
Temp1DAOImpl.java:
Code:
@Repository
public class Temp1DAOImpl extends AbstractTemp1DAO implements Temp1DAO{
private static final Log log = LogFactory.getLog(Temp1DAOImpl.class);
public Temp1DAOImpl(){
super();
log.debug("Temp1DAO created.");
}
@Autowired
public void init(final SessionFactory factory) {
setSessionFactory(factory);
}
}
AbstractTemp1DAO.java:
Code:
abstract class AbstractTemp1DAO extends _DataAccessObject<Temp1>{
private static final Log log = LogFactory.getLog(AbstractTemp1DAO.class);
@Transactional(readOnly=false)
final public void save(final Temp1 transientInstance) {
log.debug("saving Temp1 instance");
try{
getHibernateTemplate().getSessionFactory().getCurrentSession().persist(transientInstance);
log.debug("persist successful");
}
catch(final RuntimeException re){
log.error("persist failed", re);
throw re;
}
}
@Transactional(readOnly=false)
final public void delete(final Temp1 persistentInstance) {
log.debug("deleting Temp1 instance");
try{
getHibernateTemplate().getSessionFactory().getCurrentSession().delete(persistentInstance);
log.debug("delete successful");
}
catch(final RuntimeException re){
log.error("delete failed", re);
throw re;
}
}
@Transactional(readOnly=false)
final public Temp1 merge(final Temp1 detachedInstance) {
log.debug("merging Temp1 instance");
try{
final Temp1 result = (Temp1)getHibernateTemplate().getSessionFactory().getCurrentSession().merge(detachedInstance);
log.debug("merge successful");
return result;
}
catch(final RuntimeException re){
log.error("merge failed", re);
throw re;
}
}
@Transactional(readOnly=true)
final public Temp1 findById(final int id){
log.debug("getting Temp1 instance with id: " + id);
try {
final Temp1 instance = (Temp1)getHibernateTemplate().getSessionFactory().getCurrentSession().get("uk.co.company.product.persistance.hibernate.Temp1", id);
if (instance==null) {
log.debug("get successful, no instance found");
}
else {
log.debug("get successful, instance found");
}
return instance;
}
catch(final RuntimeException re){
log.error("get failed", re);
throw re;
}
}
}
If anyone has an ideas please let me know! I'm not having much luck with Google at the moment.
Cheers,
Adam