帮助文档源代码:
public class HibernateUtil { private static final SessionFactory sessionFactory = buildSessionFactory(); private static SessionFactory buildSessionFactory() { try { // Create the SessionFactory from hibernate.cfg.xml new Configuration().configure().buildSessionFactory( new StandardServiceRegistryBuilder().build() ); } catch (Throwable ex) { // Make sure you log the exception, as it might be swallowed System.err.println("Initial SessionFactory creation failed." + ex); throw new ExceptionInInitializerError(ex); } } public static SessionFactory getSessionFactory() { return sessionFactory; } }将hibernate配置通的代码:
public class HibernateUtil {
public static SessionFactory getMySessionFactory(){
StandardServiceRegistryBuilder serviceRegistryBuilder = new StandardServiceRegistryBuilder();
Configuration configuration = new Configuration().configure();
serviceRegistryBuilder.applySettings(configuration.getProperties());//添加了这样一个配置,按理说serviceRegistryBuilder默认就应该有的。但是没有
SessionFactory sessionFacotry = configuration.buildSessionFactory(
serviceRegistryBuilder.build() );
return sessionFacotry;
}
}
只有可以利用sessionFactory得到session然后使用hibernate;
希望大家多提意见