对于hibernate代码中的类型会话,session.connection()未定义

时间:2015-09-09 12:22:12

标签: java spring hibernate session

我面临的问题是session.connection()方法不被接受,而hibernate升级从hibernate 3.6到hibernate 4.1.2 ..你能提供一些替换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();    

        }

    }
}

2 个答案:

答案 0 :(得分:0)

在Hibernate 4中不推荐使用

session.connection(),您应该使用session.doWork() API。在你的情况下,像这样

    s.doWork(new Work() {

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

如果您需要从execute()方法中返回值,请使用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;
        }
    });

答案 1 :(得分:0)

您可以在会话中使用doWork。

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

或使用sessionFactory.withOptions()修改会话以包含特定连接。