Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 类型SessionFactory中的方法openSession()不适用于参数(连接)_Java_Hibernate - Fatal编程技术网

Java 类型SessionFactory中的方法openSession()不适用于参数(连接)

Java 类型SessionFactory中的方法openSession()不适用于参数(连接),java,hibernate,Java,Hibernate,当我将Hibernate 3.2.7迁移到Hibernate 4.3.5,将Java7迁移到Java8,将Tomcat7迁移到Tomcat8时,我得到了下面的两个错误。请为这个问题提供一些解决方案 类型SessionFactory中的方法openSession()不适用于参数(AuditLoginInterceptor)HibernateUtil.java 类型会话的方法connection()未定义--AuditLogInterceptor.java HibernateUtil.java pu

当我将Hibernate 3.2.7迁移到Hibernate 4.3.5,将Java7迁移到Java8,将Tomcat7迁移到Tomcat8时,我得到了下面的两个错误。请为这个问题提供一些解决方案

  • 类型SessionFactory中的方法openSession()不适用于参数(AuditLoginInterceptor)
    HibernateUtil.java

  • 类型会话的方法connection()未定义--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;
    }
    
    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();    
    
        }
    
    }
    
    AuditLogUtil.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;
    }
    
    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();    
    
        }
    
    }
    

    }

    尝试这样做可能会有所帮助:

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

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

    hmm的设置您配置sessionFactory的方式看起来不太合适,您是否在某处描述了驱动程序类名?或者至少是初始数据源?因为hibernate将在启动时在映射的POJO和相应的表之间执行一些验证,这在您的配置中是不会发生的,因为它没有正确的连接…我更改了代码..我的行有问题openSession(拦截器)HibernateUtil中的getSessionFactory方法在哪里?getSessionFactory()getter方法添加到CodeAuditLoginInterceptor拦截器=新的AuditLoginInterceptor()中;s=sessionFactory.openSession(拦截器);拦截器设置会话;上述sessionFactory.openSession(interceptor)方法的唯一问题..它在hibernate 3.6中工作。现在hibernate 4.3.1不工作了。请告诉我有什么想法或解决方案??