我插入一个宠物信息,宠物表有一个字段引用类型表里的typeId,页面里类型是做了select标签,dao层里的代码如下:public void save(PetInfo transientInstance) {
Transaction tx = null;
try {
tx = getSession().beginTransaction();
getSession().save(transientInstance);
tx.commit();
} catch (RuntimeException re) {
throw re;
}
finally
{
HibernateSessionFactory.closeSession();
}
}
宠物映射文件如下:<hibernate-mapping default-lazy="false">
    <class name="com.guangri.entity.PetInfo" table="PetInfo" schema="dbo" catalog="Epet">
        <id name="petId" type="java.lang.Integer">
            <column name="pet_id" />
            <generator class="native" />
        </id>
         <many-to-one name="typeInfo" class="com.guangri.entity.TypeInfo" cascade="none">
            <column name="pet_type"/>
        </many-to-one>
    </class>
</hibernate-mapping>类型映射文件如下:<hibernate-mapping>
    <class name="com.guangri.entity.TypeInfo" table="typeInfo" schema="dbo" catalog="Epet">
        <id name="typeId" type="java.lang.Integer">
            <column name="typeId" />
            <generator class="native" />
        </id>
        <property name="typeName" type="java.lang.String">
            <column name="typeName" length="20" not-null="true" unique="true" />
        </property>
     </class>
</hibernate-mapping>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:p="http://www.springframework.org/schema/p"
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/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
</bean>
 
 <bean id="petInfoDAO" class="com.guangri.dao.impl.hibernate.PetInfoDAO">
  <property name="sessionFactory" ref="sessionFactory"></property>
 </bean>
   
 <bean id="typeInfoDAO" class="com.guangri.dao.impl.hibernate.TypeInfoDAO">
  <property name="sessionFactory" ref="sessionFactory"></property>
 </bean>
   
 <bean name="/login" class="com.guangri.actions.PetAction">
  <property name="petInfoDAO" ref="petInfoDAO"></property>
  <property name="typeInfoDao" ref="typeInfoDAO"></property>
 </bean>
</beans>struts配置文件如下:<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd"><struts-config>
  <data-sources />
  <form-beans>
   <form-bean name="showForm" type="com.guangri.forms.ShowForm"></form-bean>
  </form-beans>
  <global-exceptions />
  <global-forwards />
  <action-mappings >
    <!-- 登陆,处理所有Action -->
     <action path="/login" type="org.springframework.web.struts.DelegatingActionProxy" name="showForm" scope="request" parameter="operator"></action>
  </action-mappings>
  <message-resources parameter="com.yourcompany.struts.ApplicationResources" />
 
 <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
  <set-property value="classpath:applicationContext.xml" property="contextConfigLocation"/>
 </plug-in>
</struts-config>执行save操作后控制台输出insert 语句,但我查数据库,控制台显示一直正在查询,我把tomcat服务关了以后数据库才查询完毕,但数据没有插入进去,请哪位大侠帮个忙,万分感谢!

解决方案 »

  1.   

    看你贴出的内容了解到,你还使用了Spring的。
    但你却又去用没有Spring的方式进行Dao层代码的编写。
    也就是说,你的sessionFactory并不是同一个实例。Spring中还能这样写?有问题吧。
    <bean name="/login" class="com.guangri.actions.PetAction">
             <property name="petInfoDAO" ref="petInfoDAO"></property>
             <property name="typeInfoDao" ref="typeInfoDAO"></property>
         </bean>
      

  2.   

    这样写没问题,使用Spring 来管理Struts 的 Action
    从LZ 的现象来看,貌似是数据库死锁了,既然用Spring 来管理Session,直接用 Spring HibernateTemplate来进行DAO的操作 和 Spring的AOP来处理事务
      

  3.   

    既然用SSH,为啥不用Spring 的DAO呢,还不用关心事务什么的
      

  4.   

    谢谢大家,刚学SSH,不太了解,就是应该用spring来控制,用getHibenateTemplate()来做,例如如下:
    PetInfo petInfo = (PetInfo)getHibernateTemplate().get(PetInfo.class, id);