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
HibernateException: Access to DialectResolutionInfo cannot be null when ‘hibernate.dialect‘ not set_第1张图片

二、问题背景

新建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


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_testproperty>
        <property name="connection.driver_class">com.mysql.jdbc.Driverproperty>
        
        <property name="dialect">org.hibernate.dialect.MySQL5Dialectproperty>
        <mapping class="com.hibernate.helloworld.TblAccountEntity"/>
        
        

        
        


    session-factory>
hibernate-configuration>

三、原因分析

缺少数据库方言配置。

四、解决方案

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

        
        <property name="dialect">org.hibernate.dialect.MySQL5Dialectproperty>

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


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_testproperty>
        <property name="connection.driver_class">com.mysql.jdbc.Driverproperty>
        
        <property name="dialect">org.hibernate.dialect.MySQL5Dialectproperty>
        <mapping class="com.hibernate.helloworld.TblAccountEntity"/>
        
        

        
        


    session-factory>
hibernate-configuration>

你可能感兴趣的:(常见报错问题,hibernate,java,后端)