我正在编辑这篇文章:我试图从另一个类 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;
}
}