0

私はまだ次のことを理解していません:

method xxx{
    Teacher t=dao.get(new Teacher(1));
    t.setName("mark"); 

    finder.find(Teacher.class,null,null,null);

    dao.getSf().getCurrentSession().evict(t);               

    t.setName("ff"); 
    return t;
}

xxxとの両方finder.findが tx の制御下にあり、伝播が必要ですが、最初の変更のみがデータベースにコミットされることがわかりました。メソッドを削除するfindと、変更はコミットされません。どうしてこれなの?

わかりました、ここに私のコードがあります:

public List<T> find(Class<T> bean, Map<String, Object> param,Pageable pg,Sortable od) {
    //from Questionair t where 1=1
    String hql="from "+this.getBeanName(bean)+" "+this.allias+" where 1=1 "+this.getWhereCourse(param)+" ";
    if(od!=null && od.getDir()!=null && od.getSort()!=null){
        hql+="order by "+this.allias+"."+od.getSort()+" "+od.getDir();
    }
    //开始查询
    Session s=sf.getCurrentSession();
    Query q=sf.getCurrentSession().createQuery(hql);
    if(pg!=null){
        q.setFirstResult(pg.getStart());
        q.setMaxResults(pg.getStart()+pg.getLimit());
    }
    System.out.println(q);
    return q.list();
}

QuestionairService の xxx メソッド:

    try{
        Teacher t=dao.get(new Teacher(1));
        t.setName("mark"); 

        //-------------
        System.out.println("加入finder");
        finder.find(Teacher.class,null,null,null);

        //-------------
        System.out.println("加入evict");
        dao.getSf().getCurrentSession().evict(t);


        t.setName("ff"); 
        return t;
    }

そして、ここに私の構成があります

<tx:advice id="txAdvice" transaction-manager="tm">
    <tx:attributes>
        <tx:method name="*" propagation="REQUIRED" />
        <!--  <tx:method name="find" propagation="REQUIRES_NEW"/> -->
    </tx:attributes>
</tx:advice>
<aop:config>
    <aop:pointcut id="allService" expression="(execution(* com.etc.ssh.service.*.*(..)))" />
    <aop:pointcut id="allFinders" expression="(execution(* com.etc.ssh.finder.*.find(..)))" />
    <aop:pointcut id="allSession" expression="(execution(* *.getCurrentHttpSession(..)))" />
    <aop:advisor pointcut-ref="allService" advice-ref="txAdvice"/>
    <aop:advisor pointcut-ref="allFinders" advice-ref="txAdvice"/>
</aop:config>

<aop:config proxy-target-class="true">
    <aop:advisor pointcut-ref="allSession" advice-ref="ssAdvice"/>
</aop:config>
4

1 に答える 1

1

呼び出すevict()と、オブジェクトがその後永続化されなくなります。それを「再永続化」するには、save()もう一度呼び出す必要があります。

于 2012-11-02T08:12:53.777 に答える