尽管资源文件夹中没有位置,Hibernate也无法找到cfg.xml。如何设置hibernate.cfg.xml的路径?
工厂类:
public class HibernateSessionFactoryUtil {
private static SessionFactory sessionFactory;
private HibernateSessionFactoryUtil() {}
public static SessionFactory getSessionFactory() {
if (sessionFactory == null) {
try {
Configuration config = new Configuration().configure();
ServiceRegistry serviceRegistery = new StandardServiceRegistryBuilder().applySettings(config.getProperties()).build();
SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistery);
} catch (Exception e) {
System.out.println("Exception!!! " + e);
}
}
return sessionFactory;
}
要点:
public class Main {
public static void main(String[] args) {
Session session = HibernateSessionFactoryUtil.getSessionFactory().openSession();
}