异常:org.hibernate.HibernateException:当 'hibernate.dialect' 未设置时,对 DialectResolutionInfo 的访问不能为空

Exception : org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set

我是 hibernate 的新手,我正在努力将它与 tomcat 服务器一起使用。这就是我得到的,代码如下:

错误日志:

        GRAVE: Exception lors de l'envoi de l'évènement contexte initialisé 
    (context initialized) à l'instance de classe d'écoute (listener) 
    [org.springframework.web.context.ContextLoaderListener]
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.hibernate.SessionFactory]: Factory method 'sessionFactory' threw exception; nested exception is org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588)
    ... 50 more
Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
    at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.determineDialect(DialectFactoryImpl.java:104)
    at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:71)
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:205)
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:111)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:234)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206)
    at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1887)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1845)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1930)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:372)
    at pro.logikal.testdbconnexionspring.configuration.DBConfig.sessionFactory(DBConfig.java:26)
    at pro.logikal.testdbconnexionspring.configuration.DBConfig$$EnhancerBySpringCGLIB$adff96.CGLIB$sessionFactory(<generated>)
    at pro.logikal.testdbconnexionspring.configuration.DBConfig$$EnhancerBySpringCGLIB$adff96$$FastClassBySpringCGLIB$b15a99a.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:356)
    at pro.logikal.testdbconnexionspring.configuration.DBConfig$$EnhancerBySpringCGLIB$adff96.sessionFactory(<generated>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)
    ... 51 more

配置文件:

@Configuration
@EnableTransactionManagement
public class DBConfig {

/**
 * Créer la base de données et associe les entités utilisables pendant la
 * session
 */

@Bean
public SessionFactory sessionFactory() {
    return new LocalSessionFactoryBuilder(getDataSource()).scanPackages("pro.logikal.testdbconnexionspring.entity")
            .buildSessionFactory();
}

/**
 * Configure l'accès à la base de données. La configuration à la base de données
 * se trouve dans le fichier de contexte (WebContent / Meta-inf / Context.xml)
 * 
 * @return Datasource
 */

@Bean
public DataSource getDataSource() {
    try {
        InitialContext initialContext = new InitialContext();
        DataSource dataSource = (DataSource) initialContext.lookup("java:/comp/env/jdbc/logdb");
        return dataSource;
    } catch (NamingException e) {
        e.printStackTrace();
        return null;
    }



}

@Bean
public HibernateTransactionManager hibernateTransactionManager() {
    return new HibernateTransactionManager(this.sessionFactory());
}


}

`Context.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<Context docBase="TestDBConnexionSpring" 
        path="TestDBConnexionSpring" reloadable="true" 
allowCasualMultiParsing="true">

<Resources cachingAllowed="true" cacheMaxSize="100000"/>

<Resource
name="jdbc/logdb"
auth="Container"
type="javax.sql.DataSource"
url="jdbc:mysql://localhost:3306/logdb"
username="root"
password="g5tn4gfs"
maxActive="200"
maxIdle="5"
maxWait="-1"
removeAbandonned="true"
removeAbandonnedTimeout="60"
logAbandonned="true">

</Resource>


</Context>

Web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
<display-name>Archetype Created Web Application</display-name>

<resource-ref>
<description>DB Connexion</description>
<res-ref-name>jdbc/logdb</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>

</resource-ref>

</web-app>

谁能告诉我哪里出了问题。看起来数据库连接可以通过 spring 以多种方式建立,但我想了解为什么这段代码可以正常工作,但当我在我的个人计算机上编写相同的代码时却不行(ide,spring 和 hibernate 共享相同的版本和配置,就我在这两种环境中所看到的而言)。这是我的 pom.xml :

<project xmlns="http://maven.apache.org/POM/4.0.0" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>pro.logikal</groupId>
<artifactId>TestDBConnexionSpring</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>TestDBConnexionSpring Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<hibernate-version>4.3.10.Final</hibernate-version>
<dbcp-version>2.1.1</dbcp-version>
<mysql-version>6.0.5</mysql-version>
<springboot-version>1.4.1.RELEASE</springboot-version>
<spring-version>4.2.0.RELEASE</spring-version>
</properties>

<dependencies>

<!-- Begin Spring -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>${springboot-version}</version>
    </dependency>

    <!-- JPA Data (We are going to use Repositories, Entities, Hibernate, etc...) -->

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
        <version>${springboot-version}</version>
    </dependency>

    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
    <version>${springboot-version}</version>
    </dependency>

    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
    <version>${springboot-version}</version>
    </dependency>

    <!-- Spring security -->

    <dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-ldap</artifactId>
    <version>${spring-version}</version>
    </dependency>

    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-websocket</artifactId>
    <version>${spring-version}</version>
    </dependency>

    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-messaging</artifactId>
    <version>${spring-version}</version>
    </dependency>

    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
    <version>${spring-version}</version>
    </dependency>


    <!-- Use MySQL Connector-J -->

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.44</version>
    </dependency>


    <dependency>
    <groupId>jstl</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>

    <dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>${hibernate-version}</version>
    </dependency>

    <dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-jpamodelgen</artifactId>
    <version>5.2.11.Final</version>
    </dependency>

    <dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>4.3.6.Final</version>
    </dependency>

    <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    </dependency>




</dependencies>





<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven.compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.8</source>
                <targert>1.8</targert>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.6</version>
            <configuration>
                <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
        </plugin>
    </plugins>
</build>
</project>

Hibernate Dialect 是一个 Class,它根据您正在使用的数据库提供预定义的配置。

您需要设置 Hibernate Dialect 以便休眠时识别要处理的数据库。你没有提到你使用的是哪个休眠版本,这个 link 确实包括最新的几乎常见的方言。这是一个示例 Hibernate 配置文件 (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="hibernate.connection.datasource">java:jboss/datasources/MySQLDS</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
        <property name="hibernate.hbm2ddl.auto">update</property>
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.format_sql">true</property>
        <property name="hibernate.jdbc.batch_size">50</property>
    </session-factory>
</hibernate-configuration>

你可以看到 hibernate.dialect 设置为 org.hibernate.dialect.MySQL5InnoDBDialect 因为我使用的是 MySQL 5 InnoDB 引擎。

即使您以编程方式配置休眠,您也已设置 hibernate.dialect

更新

这个documentation有点老了,但你会找到你想要的。