读书人

用spring的HibernateTemplate的save()

发布时间: 2013-01-26 13:47:04 作者: rapoo

用spring的HibernateTemplate的save()方法不能插入数据
本帖最后由 scriptguy 于 2010-05-19 19:35:05 编辑 在别的项目中可以直接使用HibernateTemplate的save()来持久化数据
但是现在遇到了问题:
使用HibernateTemplate的save()有hibernate插入语句:Hibernate: insert into T_Orgnization (name, sn, description, pid) values (?, ?, ?, ?)
可是查看数据库却没有相应的数据,太奇怪了!!!!!

希望高手给予解答!


DAO类:


package com.scriptguy.oa.manager.impl;

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.scriptguy.oa.manager.OrgManager;
import com.scriptguy.oa.model.Orgnization;

public class OrgManagerImpl extends HibernateDaoSupport implements OrgManager {

public void addOrg(Orgnization org, int parentId) {
this.getHibernateTemplate().save(org);
System.out.println("test");

}
}


hibernate.cfg.xml:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://127.0.0.1/oa</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">scriptguy</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.current_session_context_class">thread</property>
<mapping resource="com/scriptguy/oa/model/Orgnization.hbm.xml"/>
<mapping resource="com/scriptguy/oa/model/Person.hbm.xml"/>
</session-factory>
</hibernate-configuration>


spring配置文件:

<?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/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<!-- 配置sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>


</bean>
<bean id="orgManager" class="com.scriptguy.oa.manager.impl.OrgManagerImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
</beans>



junit测试文件:

package com.scriptguy.oa.manager;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.scriptguy.oa.manager.impl.OrgManagerImpl;
import com.scriptguy.oa.model.Orgnization;

import junit.framework.TestCase;

public class OrgManagerImplTest extends TestCase {
private BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext-*.xml");
public void testAddOrg() {
OrgManagerImpl orgmanager = (OrgManagerImpl)factory.getBean("orgManager");
Orgnization org = new Orgnization();
org.setName("test 事务管理");
org.setDescription("description");
orgmanager.addOrg(org,3);

}

public void testDeleteOrg() {
fail("Not yet implemented");
}

public void testFindOrg() {
fail("Not yet implemented");
}

public void testFindOrgs() {
fail("Not yet implemented");
}

public void testUpdateOrg() {
fail("Not yet implemented");
}

}



<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class table="T_Orgnization" name="com.scriptguy.oa.model.Orgnization">
<id name="id">
<generator class="native"/>
</id>
<property name="name"/>
<property name="sn"/>
<property name="description"/>
<many-to-one column="pid" name="parent" lazy="false"/>
<set lazy="extra" inverse="true" name="children">
<key column="pid"/>
<one-to-many class="com.scriptguy.oa.model.Orgnization"/>
</set>
</class>
</hibernate-mapping>

搞了好久都没有搞懂,查阅了许多资料,不知道为什么,
[解决办法]
你说的是你加上了事物处理了就可以了。 那就是现在的项目没有自动事物处理呗。。

这个是可以设置的啊。


可能原来的项目没有设置事物处理,但是设置了setAutoCommit(true).而你现在的项目什么都没做。
[解决办法]
可能是由于你没有写声明式事务,或者声明式事务存在问题,所以,在Eclipse里面即使是调试,会发现代码确实执行了,而且如果返回标识列的话,也能得到值,就是数据库没有更新,而且出现无响应的状态,假死状态。当你下次再插入的时候,会发现标识列在增长,数据库依旧无响应一样。。这个就是声明式事务配置错误或者没有配事务时程序引发的。。。
[解决办法]
引起这个问题是由于你的SessionFactory或HibernateTemplate为空,就是没找到
加上下面的代码运行看看

public class OrgManagerImpl extends HibernateDaoSupport implements OrgManager {
private SessionFactory mySessionFacotry;
@Resource
public void setMySessionFacotry(SessionFactory sessionFacotry) {


this.mySessionFacotry = sessionFacotry;
}
@PostConstruct
public void injectSessionFactory() {
super.setSessionFactory(mySessionFacotry);
}
public void addOrg(Orgnization org, int parentId) {
this.getHibernateTemplate().save(org);
System.out.println("test");

}
}


目的就好似为父类注入SessionFactory,曾经我也遇到这个问题

读书人网 >Java Web开发

热点推荐