2015-09-09 66 views
2

我遇到問題session.connection()方法不接受,而hibernate升級完成從休眠3.6到休眠4.1.2 ..可以提供一些解決方案替換session.connection方法?session.connection()未定義爲休眠代碼中的類型會話

AuditLogInterceptor.java:

public class AuditLogInterceptor extends EmptyInterceptor{ 

    Session session; 
    private Set inserts = new HashSet(); 
    private Set updates = new HashSet(); 
    private Set deletes = new HashSet(); 

    public void setSession(Session session) { 
     this.session=session; 
    } 

    public boolean onSave(Object entity,Serializable id, 
     Object[] state,String[] propertyNames,Type[] types) 
     throws CallbackException { 

     System.out.println("onSave"); 

     if (entity instanceof IAuditLog){ 
      inserts.add(entity); 
     } 
     return false; 

    } 

    public boolean onFlushDirty(Object entity,Serializable id, 
     Object[] currentState,Object[] previousState, 
     String[] propertyNames,Type[] types) 
     throws CallbackException { 

     System.out.println("onFlushDirty"); 

     if (entity instanceof IAuditLog){ 
      updates.add(entity); 
     } 
     return false; 

    } 

    public void onDelete(Object entity, Serializable id, 
     Object[] state, String[] propertyNames, 
     Type[] types) { 

     System.out.println("onDelete"); 

     if (entity instanceof IAuditLog){ 
      deletes.add(entity); 
     } 
    } 

    //called before commit into database 
    public void preFlush(Iterator iterator) { 
     System.out.println("preFlush"); 
    } 


    //called after committed into database 
    @SuppressWarnings("null") 
    public void postFlush(Iterator iterator) { 
     System.out.println("postFlush"); 
     TbMasUsers tmu = null; 
     try{ 

      for (Iterator it = inserts.iterator(); it.hasNext();) { 
       IAuditLog entity = (IAuditLog) it.next(); 
       System.out.println("postFlush - insert"); 

       try { 

       AuditLogUtil.LogIt("Saved",entity, session.connection()); 
        // session.doWork(entity); 
       } catch (HibernateException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } catch (Exception e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 

      for (Iterator it = updates.iterator(); it.hasNext();) { 
       IAuditLog entity = (IAuditLog) it.next(); 
       System.out.println("postFlush - update"); 
       try { 
        AuditLogUtil.LogIt("Updated",entity, session.connection()); 
       } catch (HibernateException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } catch (Exception e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 

      for (Iterator it = deletes.iterator(); it.hasNext();) { 
       IAuditLog entity = (IAuditLog) it.next(); 
       System.out.println("postFlush - delete"); 
       try { 
        AuditLogUtil.LogIt("Deleted",entity, session.connection()); 
       } catch (HibernateException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } catch (Exception e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 

     } finally { 
      inserts.clear(); 
      updates.clear(); 
      deletes.clear(); 
     } 
    } 

} 

AuditLogUtil.java:

public class AuditLogUtil{ 


    public static void LogIt(String action, 
     IAuditLog entity, Connection conn) throws Exception{ 
     SessionServiceImpl sessionServiceImpl=new SessionServiceImpl(); 
     TbMasUsers tbMasUsers=(TbMasUsers) sessionServiceImpl.getUserInSession(); 
     Integer loingEmpId=Integer.parseInt(tbMasUsers.getIntEmpId().getStrEmpId()); 

     //SessionImpl sessionImpl = (SessionImpl) session; 
     // Connection conn1 = sessionImpl.connection(); 
     Session tempSession = HibernateUtil.getSessionFactory().openSession(conn); 


     try { 
      @SuppressWarnings("null") 
      AuditLog auditRecord = new AuditLog(action,entity.getLogDeatil() 
        , new Date(),entity.getId(), entity.getClass().toString(),loingEmpId); 
      tempSession.save(auditRecord); 
      tempSession.flush(); 

     } finally { 
      tempSession.close();  

     } 

    } 
} 
+1

正如您已經嘗試過的,通過評論代碼判斷,您應該使用'session.doWork()'API。一個快速而骯髒的黑客就是將'session'轉換爲'SessionImpl',其中'connection()'方法可用,但是不推薦使用。 –

+0

它不工作,而我現在正在嘗試,如果你知道代碼session.doWork()讓我知道代碼...謝謝,, – latcha

+0

我已經添加它作爲答案,所以它可以很好地格式化 –

回答

0

session.connection()在Hibernate中4已經過時,你應該使用session.doWork() API這一點。在你的情況下,像這樣

s.doWork(new Work() { 

     @Override 
     public void execute(Connection c) throws SQLException { 
      AuditLogUtil.LogIt("Deleted",entity, c); 
     } 
    }); 

如果您需要從​​方法中返回一個值,使用s.doReturningWork()

Map<Integer, String> result = s.doReturningWork(new ReturningWork<Map<Integer, String>>() { 

     @Override 
     public Map<Integer, String> execute(Connection c) { 
      Map<Integer, String> someMap = new HashMap<Integer, String>(); 

      return someMap; 
     } 
    }); 
0

可以使用的doWork開庭。

getSession().doWork(new Work(){ 
    @Override public void execute( Connection connection) throws SQLException { 
    } 
    } 
); 

或使用sessionFactory.withOptions()修改會話以包含特定連接。