Orgnization表的配制:
<?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="test.jian.oa.Orgnization" lazy="false">
    <id name="id">
      <generator class="native"/>
    </id>
    <property name="name"/>
    <property name="sn"/>
    <property name="description"/>
<many-to-one name="parent" column="pid" lazy="false"></many-to-one>
<set name="childrens" inverse="true" lazy="false">
<key column="pid"></key>
<one-to-many  class="test.jian.oa.Orgnization"/>
</set>
  </class>
</hibernate-mapping>对Orgnization的增删改查操作类package test.jian.impl;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import test.jian.manager.orgManager;
import test.jian.oa.Orgnization;
public class orgManagerimpl extends HibernateDaoSupport implements orgManager {
public void addOrg(Orgnization org, int parentId) {
org.setParent((Orgnization)getHibernateTemplate().load(Orgnization.class, new Integer(parentId)));
getHibernateTemplate().save(org);
} public void del(int id) {
Orgnization org=(Orgnization)getHibernateTemplate().load(Orgnization.class, new Integer(id));
System.out.println(org.getName());
if(org.getChildrens().size()>0){
System.out.println("这里有错误的异常到时在收拾你");
}
getHibernateTemplate().delete(org);
} public Orgnization findOrg(int orgId) {
Orgnization org=(Orgnization)getHibernateTemplate().load(Orgnization.class, new Integer(orgId));
System.out.println(org.getName());
return org;
} public void update(Orgnization org, int parentId) {
if(parentId !=0){org.setParent((Orgnization)getHibernateTemplate().load(Orgnization.class, new Integer(parentId)));}

getHibernateTemplate().update(org); }
}测试类package test.jian.oa;
import test.jian.impl.orgManagerimpl;
import test.jian.manager.orgManager;
import org.hibernate.Session;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import junit.framework.TestCase;
public class testOrg extends TestCase {
private static BeanFactory factory=new ClassPathXmlApplicationContext("applicationContext.xml");
public void testAdd(){
orgManager orgmanager = (orgManager)factory.getBean("orgManager");
Orgnization org = new Orgnization();
org.setName("测试机构");
org.setDescription("kkkkk");
orgmanager.addOrg(org, 1);
//Orgnization org =orgmanager.findOrg(1);
//orgmanager.del(3);

}
Sprin的配制如下:
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
</bean>
 <bean id="orgManager" class="test.jian.impl.orgManagerimpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>

</beans>问题,为什么addOrg方法加不了数据到数据库,del()方法也删不了,而findOrg()方法能加载数据,请高手指点一下,如何修改。

解决方案 »

  1.   

    <?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="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory">
    <ref local="sessionFactory"/>
    </property>
    </bean>
        
        <!-- 配置事务特性 -->       
        <tx:advice id="txAdvice" transaction-manager="transactionManager">
         <tx:attributes>
         <tx:method name="add*" propagation="REQUIRED"/>
         <tx:method name="del*" propagation="REQUIRED"/>
         <tx:method name="update*" propagation="REQUIRED"/>
         <tx:method name="*" read-only="true"/>
         </tx:attributes>
        </tx:advice>
        
        <!-- 配置那些类的方法进行事务管理 -->
        <aop:config>
         <aop:pointcut id="allManagerMethod" expression="execution (* com.bjsxt.oa.manager.*.*(..))"/>
         <aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod"/>
        </aop:config> 
        <bean id="orgManager" class="test.jian.impl.orgManagerimpl">
    <property name="sessionFactory" ref="sessionFactory"/>
    </bean>   
                  
    </beans>兄弟配制成这样还是不行。
      

  2.   

    为什么能find出来,就save不进去呢,还有del也没用。
      

  3.   

    能FIND?不能SAVE,DEL?那就要问问你的数据库用户权限够不咯
      

  4.   

     <tx:method name="*" read-only="true"/> 
      

  5.   

    package test.jian.oa;
    import org.hibernate.Session;
    import org.hibernate.cfg.Configuration;
    import org.hibernate.tool.hbm2ddl.SchemaExport;
    public class OAtest  {
    public static  void main(String args[]){
    Session session=HibernateUtile.getSession();
    try {
    session.beginTransaction();
    Orgnization org = new Orgnization();
    org.setDescription("刘建平");
    org.setName("sa");
    org.setSn("aa");
    session.save(org);
    } catch (Exception e) {
    System.out.print(e);
    }
    session.getTransaction().commit();
    }
    }
    但我用这个类测试可以save数据
      

  6.   

    事物那块没有配好,你的代码我懒得看了,给你段例子<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="transactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    <!-- 定义事务规则的拦截器 -->
    <bean id="transactionInterceptor"
    class="org.springframework.transaction.interceptor.TransactionInterceptor">
    <property name="transactionManager" ref="transactionManager" />
    <property name="transactionAttributes">
    <props>
    <prop key="insert*">PROPAGATION_REQUIRED</prop>
    <prop key="del*">PROPAGATION_REQUIRED</prop>
    <prop key="add*">PROPAGATION_REQUIRED</prop>
    <prop key="up*">PROPAGATION_REQUIRED</prop>
    <prop key="save*">PROPAGATION_REQUIRED</prop>
    <prop key="get*">PROPAGATION_REQUIRED</prop>
    <prop key="show*">PROPAGATION_REQUIRED</prop>
    <prop key="mod*">PROPAGATION_REQUIRED</prop>
    <prop key="*">PROPAGATION_REQUIRED</prop>
    </props>
    </property>
    </bean>
    <!-- 添加要实现事务规则的BeanName -->
    <bean
    class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
    <property name="beanNames">
    <list>
    <value>typhoonClassesDao</value>
    <value>productionSeaSKDao</value>
    </list>
    </property>
    <property name="interceptorNames">
    <list>
    <value>transactionInterceptor</value>
    </list>
    </property>
    </bean> <bean id="hibernateTemplate"
    class="org.springframework.orm.hibernate3.HibernateTemplate">
    <property name="sessionFactory">
    <ref bean="sessionFactory"/>
    </property>
    </bean></beans>
      

  7.   

    beckhamcat1兄弟我按你的配制试了一下,还是不行,
      

  8.   

    你的包是package test.jian.impl;pointcut 定义应该是
      <aop:pointcut id="allManagerMethod" expression="execution (* test.jian.impl.*.*(..))"/> 
      

  9.   

    是你的事务没提交Transaction tran=dao.getSession().beginTransaction();
      session.save(org);
      tran.commit();配置文件<aop:config> 
        <aop:pointcut id="allManagerMethod" expression="execution (*      com.bjsxt.oa.manager.*.*(..))"/> 
        </aop:config> 
    <property name="sessionFactory" ref="sessionFactory"/> 
      

  10.   

    没注意你的包名  <aop:pointcut id="allManagerMethod" expression="execution (*      test.jian.impl.*.*(..))"/>