2015-09-04 59 views
0

当我将Hibernate 3.2.7迁移到Hibernate 4.3.5,将Java7迁移到Java8并将Tomcat7迁移到Tomcat8时,我得到了下面的2个错误。请为这个问题提供一些解决方案。SessionFactory类型的openSession()方法不适用于参数(Connection)

  1. The method openSession() in the type SessionFactory is not applicable for the arguments (AuditLogInterceptor) HibernateUtil.java

  2. The method connection() is undefined for the type Session - AuditLogInterceptor.java

HibernateUtil.java

public class HibernateUtil { 


private static Configuration configuration; 
private static ServiceRegistry serviceRegistry; 
private static SessionFactory sessionFactory; 
private static final ThreadLocal threadSession = new ThreadLocal(); 
private static final ThreadLocal threadTransaction = new ThreadLocal(); 
private static final ThreadLocal threadInterceptor = new ThreadLocal(); 

static { 
    try { 
     System.out.println("inside hibernate util before calling annotation configuration"); 
     configuration = new AnnotationConfiguration(); 
     System.out.println("inside hibernate util after calling annotation configuration before building sessionFactory"); 
     // sessionFactory = configuration.configure().buildSessionFactory(); 
     // hibernate 3.5.6 jar update version code 
     Configuration configuration = new Configuration(); 
     configuration.configure("hibernate.cfg.xml"); 
     serviceRegistry = new StandardServiceRegistryBuilder().applySettings(
       configuration.getProperties()).build(); 
     sessionFactory = configuration.buildSessionFactory(serviceRegistry); 
     System.out.println("inside hibernate util after calling annotation configuration after building sessionFactory"); 
     System.out.println("static intializer of HibenrateUtil"); 
     if(sessionFactory == null){ 
      System.out.println("session factory is null"); 
     }else{ 

      System.out.println("session factory is not null"); 
     } 
    } catch (Exception ex) { 

     ex.printStackTrace(); 
    } 
} 

public static Session getSession() throws HibernateException{ 
    //System.out.println("inside getSession method"); 

    Session s = (Session) threadSession.get(); 
    try { 
     //System.out.println("inside try getSession method"); 

     //if(s == null){ 
      //System.out.println("session is null");  
     //} 

     if (s == null || !s.isOpen()) { 
      //System.out.println("Opening new Session for this thread."); 
      AuditLogInterceptor interceptor = new AuditLogInterceptor(); 

       **s = sessionFactory.openSession(interceptor);**     
       interceptor.setSession(s); 
      } 
      threadSession.set(s); 
     //System.out.println("session :"+s); 
    } catch (HibernateException ex) { 
     throw new HibernateException(ex); 
    } 
    return s; 
} 

AuditLogUtil.ja VA

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()); 

    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();  

    } 

} 

}

+0

嗯你配置sessionFactory的方式看起来不太合适,你是在描述某个地方的驱动类名吗?或者至少是最初的数据源?因为休眠,将启动之间映射的POJO和相应的表之间执行一些验证,从您的配置不会发生,因为它没有正确的连接 – AntJavaDev

回答

0

尝试做这样可能此帮助:

Configuration configuration = new Configuration(); 
configuration=configuration.configure("hibernate.cfg.xml"); 

如果不提供Hibernate的配置位置。 Configuration将如何加载设置hibernate.cfg.xml

+0

是的...我改变了代码..im得到问题的线。 = sessionFactory.openSession(拦截器) – latcha

+0

HibernateUtil中的getSessionFactory方法在哪里? – jcool

+0

getSessionfactory()getter方法添加在代码中 – latcha

相关问题