public class HibernateUtil {
private static Configuration config;
private static final SessionFactory sessionFactory;
static {
try {
config = new Configuration().configure();
config.addClass(User.class);
sessionFactory = config.buildSessionFactory(new StandardServiceRegistryBuilder().applySettings(config.getProperties()).build());
} catch (Throwable e) {
throw new ExceptionInInitializerError(e);
}
}
public static Session openSession() {
return sessionFactory.openSession();
}
private static Configuration config;
private static final SessionFactory sessionFactory;
static {
try {
config = new Configuration().configure();
config.addClass(User.class);
sessionFactory = config.buildSessionFactory(new StandardServiceRegistryBuilder().applySettings(config.getProperties()).build());
} catch (Throwable e) {
throw new ExceptionInInitializerError(e);
}
}
public static Session openSession() {
return sessionFactory.openSession();
}
}
config.addClass(User.class);//配置后,就不用在hibernate.cfg.xml加上<mapping resource="/com/test/xbt/entity/User.hbm.xml" />