2

我正在编辑这篇文章:我试图从另一个类 DbDriverManager 中“databaseUrl”,但它给了我错误,如果你看到我的代码创建设置工厂块是静态的 {} 我认为这是一个问题。你能建议我更好的方法吗

我正在解决这个错误:无法初始化类 com.xxx.YY.hn.util.HibernateUtil

SessionFactory 初始创建 error.java.lang.NullPointerException 这是我的代码:

public class DbDriverManager {


public String databaseUrl;

public String getDatabaseUrl() {
    return databaseUrl;
}

public void setDatabaseUrl() {
    FacesContext context = javax.faces.context.FacesContext.getCurrentInstance();
    HttpServletRequest request = (HttpServletRequest)context.getExternalContext().getRequest();
    this.databaseUrl = (String)request.getSession().getAttribute("databaseUrl");
}

}

public class HibernateUtil 

{

 private static final SessionFactory sessionFactory;

  static 
  {    //create sessionFactory only once    
    try 
    {

        Configuration cfg = null;
        DbDriverManager dm = new DbDriverManager();

        dm.setDatabaseUrl();

      // creating the SessionFactory from hibernate.cfg.xml
        cfg =    new AnnotationConfiguration().configure();

        System.out.println("dm.getDatabaseUrl() :" + dm.getDatabaseUrl());

        cfg.setProperty("hibernate.connection.driver_class", "net.sourceforge.jtds.jdbc.Driver");
        cfg.setProperty("hibernate.connection.url", dm.getDatabaseUrl());
        cfg.setProperty("hibernate.connection.username", "wf_dbo");
        cfg.setProperty("hibernate.connection.password", "webflex");            
        cfg.setProperty("hibernate.dialect", "org.hibernate.dialect.SQLServerDialect");
        cfg.configure();

        sessionFactory = cfg.buildSessionFactory();
        //sessionFactory = new cfg.buildSessionFactory();
      //sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();


    } 
    catch (Throwable ex) 
    {
           System.out.println("SessionFactory initial creation error."+ ex);
      throw new ExceptionInInitializerError(ex);
    }
  }
  public static SessionFactory getSessionFactory() {
     return sessionFactory;
  }

}

4

1 回答 1

1

您的代码需要稍作修改,如下所示

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

public static SessionFactory getSessionFactory(String ip) {

        SessionFactory sf = null;
        Configuration configuration = new Configuration();

        configuration.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
        configuration.setProperty("hibernate.connection.url", "jdbc:mysql://" + ip + ":3306/dbName");
        configuration.setProperty("hibernate.connection.username", "username");
        configuration.setProperty("hibernate.connection.password", "password");
        configuration.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");

        StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().
                applySettings(configuration.getProperties());
        sf = configuration.buildSessionFactory(builder.build());

        return sf;
    }

不要调用空参数配置()。我希望它在默认类路径中查找 hibernate.cfg.xml。

此外,不推荐使用 AnnotationConfiguration()。请使用休眠 4.3 或更高版本。以上代码适用于休眠 4.3.6

于 2014-12-19T08:45:21.657 回答