仅当使用不建议使用的buildSessionFactory()时,才读取hibernate.cfg.xml,而不与StandardServiceRegistryBuilder一起使用

时间:2019-06-10 18:47:44

标签: java spring hibernate jpa

我开始学习休眠并面临一个非常奇怪的问题。我在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>

使用支持的版本时记录日志 enter image description here enter image description here

在使用不推荐使用的版本时记录 enter image description here enter image description here enter image description here

0 个答案:

没有答案