getHibernateTemplate().save 不能写入数据
spring配置文件中的内容如下:<beanid="dataSource"class="com.mchange.v2.c3p0.ComboPooledDataSource...
spring配置文件中的内容如下:
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass"
value="com.microsoft.sqlserver.jdbc.SQLServerDriver">
</property>
<property name="jdbcUrl"
value="jdbc:sqlserver://localhost:1433;DatabaseName=GPSRecord">
</property>
<property name="user" value="sa"></property>
<property name="password" value="sa"></property>
</bean>
<bean id="sessionfactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.SQLServerDialect
</prop>
</props>
</property>
</bean>
<bean id="AlarmTableDAO" class="test.AlarmTableDAO">
<property name="sessionFactory">
<ref bean="sessionfactory" />
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref ="sessionFactory"/>
</bean>
<bean id="AlarmTableDAOProxy"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref local="transactionManager"/>
</property>
<property name="target" ref="AlarmTableDAO"/>
<property name="transactionAttributes">
<props>
<prop key="save*">PROPAGATION_REQUIRED </prop>
</props>
</property>
</bean>
</beans>
AlarmTableDAOImpl类中有如下代码:
public class AlarmTableDAO extends HibernateDaoSupport implements IAlarmTableDao {
private static final Log log = LogFactory.getLog(AlarmTableDAO.class);
public void save(AlarmTable transientInstance) {
log.debug("saving AlarmTable instance");
try {
getHibernateTemplate().save(transientInstance);log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
}
}
Hibernate.cfg.xml内容如下:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools.-->
<hibernate-configuration>
<session-factory>
<property name="dialect">
org.hibernate.dialect.SQLServerDialect
</property>
<property name="connection.url">
jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=GPSRecord
</property>
<property name="connection.username">sa</property>
<property name="connection.password">sa</property>
<property name="connection.driver_class">
com.microsoft.jdbc.sqlserver.SQLServerDriver
</property>
<property name="connection.autocommit">true </property>
<property name="myeclipse.connection.profile">m</property>
<mapping resource="test/AlarmTable.hbm.xml" />
</session-factory>
</hibernate-configuration>
save()函数保存一个对象,我生成一个AlarmTable对象后,调用save(),结果显示:
DEBUG [Thread-1] (AlarmTableDAO.java:33) - saving AlarmTable instance
ERROR [Thread-1] (AlarmTableDAO.java:38) - save failed
java.lang.NullPointerException
at test.AlarmTableDAO.save(AlarmTableDAO.java:35)
at test.thread.run(thread.java:60)
Exception in thread "Thread-1" java.lang.NullPointerException
at test.AlarmTableDAO.save(AlarmTableDAO.java:35)
at test.thread.run(thread.java:60) 展开
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass"
value="com.microsoft.sqlserver.jdbc.SQLServerDriver">
</property>
<property name="jdbcUrl"
value="jdbc:sqlserver://localhost:1433;DatabaseName=GPSRecord">
</property>
<property name="user" value="sa"></property>
<property name="password" value="sa"></property>
</bean>
<bean id="sessionfactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.SQLServerDialect
</prop>
</props>
</property>
</bean>
<bean id="AlarmTableDAO" class="test.AlarmTableDAO">
<property name="sessionFactory">
<ref bean="sessionfactory" />
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref ="sessionFactory"/>
</bean>
<bean id="AlarmTableDAOProxy"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref local="transactionManager"/>
</property>
<property name="target" ref="AlarmTableDAO"/>
<property name="transactionAttributes">
<props>
<prop key="save*">PROPAGATION_REQUIRED </prop>
</props>
</property>
</bean>
</beans>
AlarmTableDAOImpl类中有如下代码:
public class AlarmTableDAO extends HibernateDaoSupport implements IAlarmTableDao {
private static final Log log = LogFactory.getLog(AlarmTableDAO.class);
public void save(AlarmTable transientInstance) {
log.debug("saving AlarmTable instance");
try {
getHibernateTemplate().save(transientInstance);log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
}
}
Hibernate.cfg.xml内容如下:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools.-->
<hibernate-configuration>
<session-factory>
<property name="dialect">
org.hibernate.dialect.SQLServerDialect
</property>
<property name="connection.url">
jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=GPSRecord
</property>
<property name="connection.username">sa</property>
<property name="connection.password">sa</property>
<property name="connection.driver_class">
com.microsoft.jdbc.sqlserver.SQLServerDriver
</property>
<property name="connection.autocommit">true </property>
<property name="myeclipse.connection.profile">m</property>
<mapping resource="test/AlarmTable.hbm.xml" />
</session-factory>
</hibernate-configuration>
save()函数保存一个对象,我生成一个AlarmTable对象后,调用save(),结果显示:
DEBUG [Thread-1] (AlarmTableDAO.java:33) - saving AlarmTable instance
ERROR [Thread-1] (AlarmTableDAO.java:38) - save failed
java.lang.NullPointerException
at test.AlarmTableDAO.save(AlarmTableDAO.java:35)
at test.thread.run(thread.java:60)
Exception in thread "Thread-1" java.lang.NullPointerException
at test.AlarmTableDAO.save(AlarmTableDAO.java:35)
at test.thread.run(thread.java:60) 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询