Final Initialized 问题

The blank final field sessionFactory may not have been initialized

 

这个错,很操蛋,刚开始上网找,找了很久,所以留下作为笔记使用。

throw new HibernateException(e.getMessage());

eclipse编译private static final SessionFactory sessionFactory;这行就会报错,报错如下:
Multiple markers at this line
- The value of the field TestHibernate.sessionFactory is 
 not used
- The blank final field sessionFactory may not have been 
 initialized

 

解决方法一:
private static final SessionFactory sessionFactory;

static{
try {
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new HibernateException(e.getMessage());
}

}
}

 

 

解决方法2:

static{
try {
sessionFactory = new Configuration().configure().buildSessionFactory();
catch (HibernateException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
//throw new HibernateException(e.getMessage());
sessionFactory = null//这里初始化一下
}
posted @ 2014-12-09 12:59  ClodThunder  阅读(1284)  评论(0)    收藏  举报