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

时间:2017-10-24 04:10:46

标签: java hibernate-4.x

我是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$$43adff96.CGLIB$sessionFactory$2(<generated>)
    at pro.logikal.testdbconnexionspring.configuration.DBConfig$$EnhancerBySpringCGLIB$$43adff96$$FastClassBySpringCGLIB$$9b15a99a.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$$43adff96.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>

谁能告诉我什么是错的。看起来db connexion可以用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>

1 个答案:

答案 0 :(得分:2)

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

你需要设置一个Hibernate方言来休眠以识别它要处理的数据库。你还没有提到你正在使用哪个hibernate版本,这个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 Engine。

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

<强>更新

documentation有点旧,但你会找到你想要的东西。