用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");
}
}
<?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>
<?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>
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>
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");
}
}
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 ");
}
}
2.正常使用spring的容器来管理sessionfactory的时候还需要事物来处理相关查询的数据CURD
如果需要达到数据入库,一下建议:
1.不想要spring提供的事物,在会发生数据更改的情况下使用commit将其提交,如果不提交的情况下如果现在的情况,用debug跟踪,是没有任何问题
2.需要使用事物去处理相关的数据库操作,在spring的配置文件中,配置相关的事物操作信息,或是通过该annotation的方法来实现也是一样的。
[其他解释]