我开始学习休眠并面临一个非常奇怪的问题。我在hibernate.cfg.xml
中设置了src/main/resources
。当我使用弃用的configuration.configure().buildSessionFactory()
创建会话工厂时,将读取xml并运行程序。但是,当我使用受支持的configuration.buildSessionFactory(new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build());
时,不会读取xml并抛出错误。
HibernateUtil.class
package com.infiniteskills.data;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.Service;
import org.hibernate.service.ServiceRegistry;
import com.infiniteskills.data.entities.User;
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try
{
Configuration configuration = new Configuration();
//return configuration.buildSessionFactory(new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build());
return configuration.configure().buildSessionFactory();
}
catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("There was an error building the factory");
}
}
public static SessionFactory getSessionFactory()
{
return sessionFactory;
}
}
hibernate.cfg.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/ifinances</property>
<property name="connection.username">infinite</property>
<property name="connection.password">skills</property>
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="show_sql">true</property>
<mapping class="com.infiniteskills.data.entities.User" />
</session-factory>
</hibernate-configuration>