2013-06-18 4 views
1

Я следую tutorial, чтобы реализовать hibernate в моем проекте, хотя я установил диалект в своем файле конфигурации, но он претерпел следующую ошибку.Мой код запускается в Hibernate Dialect должен быть явно установлен

мой hibernate.cfg.xml в вебе-инфе> Классы

<?xml version='1.0' encoding='utf-8'?> 
<!DOCTYPE hibernate-configuration PUBLIC 
     "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
     "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 

<hibernate-configuration> 

    <session-factory> 

     <!-- Database connection settings --> 
     <property name="connection.driver_class"> 
      com.mysql.jdbc.Driver 
     </property> 
     <property name="connection.url"> 
      jdbc:mysql://localhost:3306/Project1 
     </property> 
     <property name="connection.username">root</property> 
     <property name="connection.password"></property> 

     <!-- JDBC connection pool (use the built-in) --> 
     <property name="connection.pool_size">1</property> 

     <!-- SQL dialect --> 
     <property name="dialect"> 
      org.hibernate.dialect.MySQLDialect 
     </property> 

     <!-- Enable Hibernate's automatic session context management --> 
     <property name="current_session_context_class">thread</property> 

     <!-- Disable the second-level cache --> 
     <property name="cache.provider_class"> 
      org.hibernate.cache.NoCacheProvider 
     </property> 

     <!-- Echo all executed SQL to stdout --> 
     <property name="show_sql">true</property> 

     <!-- Drop and re-create the database schema on startup --> 
     <property name="hbm2ddl.auto">update</property> 

       <mapping class="com.project.class1" /> 
       <mapping class="com.project.class2" /> 

    </session-factory> 

</hibernate-configuration> 

HbernateUtil.java

package com.project.util; 

import org.hibernate.*; 
import org.hibernate.cfg.*; 

public class HibernateUtil { 


    private static final SessionFactory sessionFactory; 

    static { 
     try { 

      sessionFactory = new AnnotationConfiguration().buildSessionFactory(); 
     } catch (Throwable ex) { 
      // Log exception! 
      throw new ExceptionInInitializerError(ex); 
     } 
    } 

    public static Session getSession(){ 
     try{ 
     return sessionFactory.openSession(); 
     }catch(HibernateException h){ 
      h.printStackTrace(); 
     } 
     return sessionFactory.openSession(); 
    } 
} 

pom.xml

<properties> 
     <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir> 
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
    </properties> 

    <dependencies> 
     <dependency> 
      <groupId>org.slf4j</groupId> 
      <artifactId>slf4j-api</artifactId> 
      <version>1.4.2</version> 
     </dependency> 
     <dependency> 
      <groupId>javassist</groupId> 
      <artifactId>javassist</artifactId> 
      <version>3.9.0.GA</version> 
     </dependency> 
     <dependency> 
      <groupId>log4j</groupId> 
      <artifactId>log4j</artifactId> 
      <version>1.2.15</version> 
      <exclusions> 
       <exclusion> 
        <artifactId>mail</artifactId> 
        <groupId>javax.mail</groupId> 
       </exclusion> 
       <exclusion> 
        <artifactId>jmxtools</artifactId> 
        <groupId>com.sun.jdmk</groupId> 
       </exclusion> 
       <exclusion> 
        <artifactId>jmxri</artifactId> 
        <groupId>com.sun.jmx</groupId> 
       </exclusion> 
       <exclusion> 
        <artifactId>jms</artifactId> 
        <groupId>javax.jms</groupId> 
       </exclusion> 
      </exclusions> 
     </dependency> 
     <dependency> 
      <groupId>commons-digester</groupId> 
      <artifactId>commons-digester</artifactId> 
      <version>2.0</version> 
     </dependency> 
     <dependency> 
      <groupId>commons-collections</groupId> 
      <artifactId>commons-collections</artifactId> 
      <version>3.2</version> 
     </dependency> 
     <dependency> 
      <groupId>org.freemarker</groupId> 
      <artifactId>freemarker</artifactId> 
      <version>2.3.15</version> 
     </dependency> 
     <dependency> 
      <groupId>mysql</groupId> 
      <artifactId>mysql-connector-java</artifactId> 
      <version>5.1.10</version> 
     </dependency> 
     <dependency> 
      <groupId>commons-logging</groupId> 
      <artifactId>commons-logging</artifactId> 
      <version>1.0.4</version> 
     </dependency> 
     <dependency> 
      <groupId>ognl</groupId> 
      <artifactId>ognl</artifactId> 
      <version>2.7.3</version> 
      <exclusions> 
       <exclusion> 
        <artifactId>javassist</artifactId> 
        <groupId>jboss</groupId> 
       </exclusion> 
      </exclusions> 
     </dependency> 
<!--  <dependency> 
      <groupId>org.hibernate</groupId> 
      <artifactId>hibernate-annotations</artifactId> 
      <version>3.5.0-Final</version> 
      <exclusions> 
       <exclusion> 
        <artifactId>persistence-api</artifactId> 
        <groupId>javax.persistence</groupId> 
       </exclusion> 
       <exclusion> 
        <artifactId>ehcache</artifactId> 
        <groupId>net.sf.ehcache</groupId> 
       </exclusion> 
      </exclusions> 
     </dependency>--> 
     <dependency> 
      <groupId>commons-fileupload</groupId> 
      <artifactId>commons-fileupload</artifactId> 
      <version>1.2.1</version> 
     </dependency> 
     <dependency> 
      <groupId>org.apache.struts</groupId> 
      <artifactId>struts2-core</artifactId> 
      <version>2.1.8</version> 
     </dependency> 
<!--  <dependency> 
      <groupId>org.hibernate</groupId> 
      <artifactId>ejb3-persistence</artifactId> 
      <version>1.0.1.GA</version> 
     </dependency>--> 
     <dependency> 
      <groupId>com.opensymphony</groupId> 
      <artifactId>xwork-core</artifactId> 
      <version>2.1.6</version> 
      <exclusions> 
       <exclusion> 
        <artifactId>spring-test</artifactId> 
        <groupId>org.springframework</groupId> 
       </exclusion> 
      </exclusions> 
     </dependency> 
     <dependency> 
      <groupId>dom4j</groupId> 
      <artifactId>dom4j</artifactId> 
      <version>1.6.1</version> 
      <exclusions> 
       <exclusion> 
        <artifactId>xml-apis</artifactId> 
        <groupId>xml-apis</groupId> 
       </exclusion> 
      </exclusions> 
     </dependency> 
<!--  <dependency> 
      <groupId>org.hibernate</groupId> 
      <artifactId>hibernate-core</artifactId> 
      <version>3.2.1.GA</version> 
      <exclusions> 
       <exclusion> 
        <artifactId>hibernate-jpa-2.0-api</artifactId> 
        <groupId>org.hibernate.javax.persistence</groupId> 
       </exclusion> 
       <exclusion> 
        <artifactId>jboss-logging</artifactId> 
        <groupId>org.jboss.logging</groupId> 
       </exclusion> 
       <exclusion> 
        <artifactId>jboss-transaction-api_1.1_spec</artifactId> 
        <groupId>org.jboss.spec.javax.transaction</groupId> 
       </exclusion> 
       <exclusion> 
        <artifactId>javassist</artifactId> 
        <groupId>org.javassist</groupId> 
       </exclusion> 
      </exclusions> 
     </dependency>--> 
     <dependency> 
      <groupId>org.slf4j</groupId> 
      <artifactId>slf4j-log4j12</artifactId> 
      <version>1.4.2</version> 
     </dependency> 
     <dependency> 
      <groupId>commons-beanutils</groupId> 
      <artifactId>commons-beanutils</artifactId> 
      <version>1.7.0</version> 
     </dependency> 
     <dependency> 
      <groupId>antlr</groupId> 
      <artifactId>antlr</artifactId> 
      <version>2.7.6</version> 
     </dependency> 
     <dependency> 
      <groupId>javax.transaction</groupId> 
      <artifactId>jta</artifactId> 
      <version>1.1</version> 
     </dependency> 
      <dependency> 
     <groupId>org.apache.zookeeper</groupId> 
     <artifactId>zookeeper</artifactId> 
     <version>3.3.2</version> 
     <exclusions> 
      <exclusion> 
       <groupId>com.sun.jmx</groupId> 
       <artifactId>jmxri</artifactId> 
      </exclusion> 
      <exclusion> 
       <groupId>com.sun.jdmk</groupId> 
       <artifactId>jmxtools</artifactId> 
      </exclusion> 
      <exclusion> 
       <groupId>javax.jms</groupId> 
       <artifactId>jms</artifactId> 
      </exclusion> 
      <exclusion> 
       <artifactId>junit</artifactId> 
       <groupId>junit</groupId> 
      </exclusion> 
      <exclusion> 
       <artifactId>jline</artifactId> 
       <groupId>jline</groupId> 
      </exclusion> 
     </exclusions> 
    </dependency> 
     <dependency> 
      <groupId>org.hibernate</groupId> 
      <artifactId>hibernate</artifactId> 
      <version>3.2.1.ga</version> 
     </dependency> 
     <dependency> 
      <groupId>org.hibernate</groupId> 
      <artifactId>hibernate-annotations</artifactId> 
      <version>3.5.6-Final</version> 
     </dependency> 
     <dependency> 
      <groupId>cglib</groupId> 
      <artifactId>cglib</artifactId> 
      <version>3.0</version> 
     </dependency> 
     <dependency> 
      <groupId>javax</groupId> 
      <artifactId>javaee-web-api</artifactId> 
      <version>6.0</version> 
      <scope>provided</scope> 
     </dependency> 
    </dependencies> 

    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <version>2.0</version> 
       <configuration> 
        <source>1.6</source> 
        <target>1.6</target> 
        <compilerArguments> 
         <endorseddirs>${endorsed.dir}</endorseddirs> 
        </compilerArguments> 
       </configuration> 
      </plugin> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-war-plugin</artifactId> 
       <version>2.1.1</version> 
       <configuration> 
        <failOnMissingWebXml>false</failOnMissingWebXml> 
       </configuration> 
      </plugin> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-dependency-plugin</artifactId> 
       <version>2.1</version> 
       <executions> 
        <execution> 
         <phase>validate</phase> 
         <goals> 
          <goal>copy</goal> 
         </goals> 
         <configuration> 
          <outputDirectory>${endorsed.dir}</outputDirectory> 
          <silent>true</silent> 
          <artifactItems> 
           <artifactItem> 
            <groupId>javax</groupId> 
            <artifactId>javaee-endorsed-api</artifactId> 
            <version>6.0</version> 
            <type>jar</type> 
           </artifactItem> 
          </artifactItems> 
         </configuration> 
        </execution> 
       </executions> 
      </plugin> 
     </plugins> 
    </build> 
</project> 

ответ

2

Prepend hibernate. во всех имена свойств.

<session-factory> 

    <!-- Database connection settings --> 
    <property name="hibernate.connection.driver_class"> 
     com.mysql.jdbc.Driver 
    </property> 
    <property name="hibernate.connection.url"> 
     jdbc:mysql://localhost:3306/Project1 
    </property> 
    <property name="hibernate.connection.username">root</property> 
    <property name="hibernate.connection.password"></property> 

    <!-- JDBC connection pool (use the built-in) --> 
    <property name="hibernate.connection.pool_size">1</property> 

    <!-- SQL dialect --> 
    <property name="hibernate.dialect"> 
     org.hibernate.dialect.MySQLDialect 
    </property> 

    <!-- Enable Hibernate's automatic session context management --> 
    <property name="hibernate.current_session_context_class">thread</property> 

    <!-- Disable the second-level cache --> 
    <property name="hibernate.cache.provider_class"> 
     org.hibernate.cache.NoCacheProvider 
    </property> 

    <!-- Echo all executed SQL to stdout --> 
    <property name="hibernate.show_sql">true</property> 

    <!-- Drop and re-create the database schema on startup --> 
    <property name="hibernate.hbm2ddl.auto">update</property> 

      <mapping class="com.project.class1" /> 
      <mapping class="com.project.class2" /> 

</session-factory> 

EDIT

Проверьте, есть ли у вас hibernate.cfg.xml файл в пути к классам. И измените следующий код. Необходимо позвонить configure() перед вызовом buildSessionFactory().

static { 
    try { 

     sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory(); 
    } catch (Throwable ex) { 
     // Log exception! 
     throw new ExceptionInInitializerError(ex); 
    } 
} 
+0

Я сделал это, но он по-прежнему сталкивается с такой же ошибкой. –

+0

Вы уверены, что файл, который вы редактировали, используется? Если это в веб-приложении, то вы перераспределили? – shazin

+0

Я сделал, но все еще не работает –

Смежные вопросы