如果写成这样的形式就没有错:
 private static final SessionFactory sessionFactory;    static {
        try {
            sessionFactory = new Configuration().configure()
                    .buildSessionFactory();
        } catch (HibernateException ex) {
            throw new RuntimeException("Exception building SessionFactory: "
                    + ex.getMessage(), ex);
        }
    }
sessionFactory = new Configuration().configure()
                    .buildSessionFactory();
上述语句必须放在try{}catch{}中,所以你如果仅用static来修饰sessionFactory 变量也是不行的。