2 baidu 36201753 baidu_36201753 于 2016.09.21 17:46 提问

hibernate的saveOrUpdate监听器加入后save方法就包空指针

public int save(User user) {
user.setCreateDate(new Date());
Session session=this.getSession();
Transaction tran=session.beginTransaction();
int count=(Integer)session.save(user);
tran.commit();
}
这段保存代码是可以执行的。

但是加入监听器之后session.save就会抛出空指针。

监听器代码如下:

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.hbm2ddl.auto">none</prop>
            <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.format_sql">false</prop>
        </props>
    </property>

    <property name="mappingDirectoryLocations">
        <list>
            <value>classpath:com/haier/hibernate/hbm</value>
        </list>
    </property>

    <!-- 监听器 -->
    <property name="eventListeners">  
        <map>  
            <entry key="save">
                <ref bean="saveOrUpdate" />
            </entry>
        </map>
    </property>  
</bean>

<bean id="saveOrUpdate" class="com.haier.hibernate.listener.SaveOrUpdateListener" />

    package com.haier.hibernate.listener;

import org.hibernate.HibernateException;
import org.hibernate.event.SaveOrUpdateEvent;
import org.hibernate.event.SaveOrUpdateEventListener;

public class SaveOrUpdateListener implements SaveOrUpdateEventListener {
private static final long serialVersionUID = 6050785018061727792L;

public void onSaveOrUpdate(SaveOrUpdateEvent arg0) throws HibernateException {
    System.out.println("saveOrUpdate");
}

}

saveOrUpdate
2016-09-21 17:31:45,442 DEBUG org.springframework.jdbc.datasource.DataSourceTransactionManager.processRollback:847 - Initiating transaction rollback
2016-09-21 17:31:45,442 DEBUG org.springframework.jdbc.datasource.DataSourceTransactionManager.doRollback:284 - Rolling back JDBC transaction on Connection [oracle.jdbc.driver.T4CConnection@cc49d2]
2016-09-21 17:31:45,442 DEBUG org.springframework.jdbc.datasource.DataSourceTransactionManager.doCleanupAfterCompletion:327 - Releasing JDBC Connection [oracle.jdbc.driver.T4CConnection@cc49d2] after transaction
2016-09-21 17:31:45,442 DEBUG org.springframework.jdbc.datasource.DataSourceUtils.doReleaseConnection:327 - Returning JDBC Connection to DataSource
2016-09-21 17:31:45,442 ERROR org.apache.struts2.dispatcher.Dispatcher.error:38 - Exception occurred during processing request: null
java.lang.NullPointerException
at com.haier.hibernate.user.dao.impl.UserDaoImpl.save(UserDaoImpl.java:53)
at com.haier.hibernate.user.service.impl.UserServiceImpl.save(UserServiceImpl.java:25)

    还请各位大哥、大神赐教!

1个回答

dabocaiqq
dabocaiqq   2016.10.04 00:22
Csdn user default icon
上传中...
上传图片
插入图片