HibernateException: Access to DialectResolutionInfo cannot be null when ‘hibernate.dialect‘ not set

Hibernate 启动报错系列
(一)缺少数据库的方言配置:Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when ‘hibernate.dialect’ not set
(二)缺少数据库的用户名和密码配置:Caused by: java.sql.SQLException: Access denied for user ‘‘@‘localhost‘ (using password: NO)

一、报错问题

Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when ‘hibernate.dialect’ not set
在这里插入图片描述

二、问题背景

新建Java项目,并添加 Hibernate 框架支持,启动测试(运行默认的Main类中的main()方法),出现报错。

Main.java

import org.hibernate.HibernateException;
import org.hibernate.Metamodel;
import org.hibernate.query.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import javax.persistence.metamodel.EntityType;

import java.util.Map;

public class Main {
    
    
    private static final SessionFactory ourSessionFactory;

    static {
    
    
        try {
    
    
            Configuration configuration = new Configuration();
            configuration.configure();

            ourSessionFactory = configuration.buildSessionFactory();
        } catch (Throwable ex) {
    
    
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static Session getSession() throws HibernateException {
    
    
        return ourSessionFactory.openSession();
    }

    public static void main(final String[] args) throws Exception {
    
    
        final Session session = getSession();
        try {
    
    
            System.out.println("querying all the managed entities...");
            final Metamodel metamodel = session.getSessionFactory().getMetamodel();
            for (EntityType<?> entityType : metamodel.getEntities()) {
    
    
                final String entityName = entityType.getName();
                final Query query = session.createQuery("from " + entityName);
                System.out.println("executing: " + query.getQueryString());
                for (Object o : query.list()) {
    
    
                    System.out.println("  " + o);
                }
            }
        } finally {
    
    
            session.close();
        }
    }
}

hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="connection.url">jdbc:mysql://localhost:3306/hibernate_test</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <!-- 配置hibernate的基本信息 -->
        <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
        <mapping class="com.hibernate.helloworld.TblAccountEntity"/>
        <!-- <property name="connection.username"/> -->
        <!-- <property name="connection.password"/> -->

        <!-- DB schema will be updated if needed -->
        <!-- <property name="hibernate.hbm2ddl.auto">update</property> -->


    </session-factory>
</hibernate-configuration>

三、原因分析

缺少数据库方言配置。

四、解决方案

在 Hibernate 的配置文件 hibernate.cfg.xml 中,添加数据库方言配置。MySQL的方言配置如下所示:

        <!-- 配置hibernate的基本信息 -->
        <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>

完整示例如下:
hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="connection.url">jdbc:mysql://localhost:3306/hibernate_test</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <!-- 配置hibernate的基本信息 -->
        <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
        <mapping class="com.hibernate.helloworld.TblAccountEntity"/>
        <!-- <property name="connection.username"/> -->
        <!-- <property name="connection.password"/> -->

        <!-- DB schema will be updated if needed -->
        <!-- <property name="hibernate.hbm2ddl.auto">update</property> -->


    </session-factory>
</hibernate-configuration>

猜你喜欢

转载自blog.csdn.net/Shipley_Leo/article/details/130795242
今日推荐