JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Object Relational Mapping » Hibernate  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to postflat modego to previous topicgo to next topic
本主題所含的標籤
作者 doInHibernate如何丟出例外給Action?
phuang3





發文: 38
積分: 0
於 2010-05-25 17:07 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
請問在doInHibernate中如何傳出RuntimeException至上層的action? 因為我需要在action中處理此DAO的exception然後回傳錯誤訊息給前端client。目前我是利用Spring的HibernateTemplate處理,遇到問題時要throw RuntimeException讓程式rollback(預設只有RuntimeException才會rollback)且讓action知道有exception發生。無奈,doInHibernate callback程式會由HibernateTemplate強制catch exception所以無法傳達到action中,不知道有何辦法可以解決這個問題? 感謝!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
public class OuSetupDAO 
{
  @Resource(name="hibernateTemplate") private HibernateTemplate template;
  
  @Transactional(propagation=Propagation.REQUIRED, readOnly=false, isolation=Isolation.DEFAULT, timeout=20)
  public void removeSysUser(final String userUuid)
  {
    template.execute(new HibernateCallback()
    {
      @SuppressWarnings("unchecked")
      public Object doInHibernate(Session session) throws HibernateException 
      {
        //condition check
        List<Object> tx = (List<Object>) session.createCriteria(Tx.class).add(Restrictions.eq("sysUser.userUuid", userUuid)).setProjection(Projections.rowCount()).list();
        long tx_count = (Long) DataAccessUtils.longResult(tx);
        if(tx_count>0)
          throw new RuntimeException("User with id:"+userUuid+" has participated in existing transactions.");
        
        List<Object> tx_update=(List<Object>)session.createCriteria(Tx.class).add(Restrictions.eq("lastUpdater", userUuid)).setProjection(Projections.rowCount()).list();
        long tx_update_count=(Long)DataAccessUtils.longResult(tx_update);
        if(tx_update_count>0)
          throw new RuntimeException("User with id:"+userUuid+" has participated in updating transactions.");
 
        List<Object> prod_update=(List<Object>)session.createCriteria(Product.class).add(Restrictions.eq("lastUpdater", userUuid)).setProjection(Projections.rowCount()).list();
        long prod_update_count=(Long)DataAccessUtils.longResult(prod_update);
        if(prod_update_count>0)
          throw new RuntimeException("User with id:"+userUuid+" has participated in updating products.");
 
        SysUser u=(SysUser)session.get(SysUser.class,userUuid);
        if(u==null)
          throw new RuntimeException("Missing user with id:"+userUuid);
        
        //remove user
        session.createQuery("delete SysPreference p where p.sysUser.userUuid=:uuid").setString("uuid", u.getUserUuid()).executeUpdate();
        session.createQuery("delete UserAuth u where u.compId.userUuid=:uuid").setString("uuid", u.getUserUuid()).executeUpdate();
        session.createQuery("delete SysPhrase p where p.compId.phraseUuid=:uuid").setString("uuid", u.getUserName()).executeUpdate();
        session.delete(u);
        return null;
      }
    });
  }
}


reply to postreply to post
話題樹型展開
人氣 標題 作者 字數 發文時間
1148 doInHibernate如何丟出例外給Action? phuang3 2541 2010-05-25 17:07
1200 Re:doInHibernate如何丟出例外給Action? phuang3 48 2010-05-25 17:19
» JWorld@TW »  Object Relational Mapping » Hibernate

reply to postflat modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8