java hibernate 4 org.hibernate.HibernateException:未找到方言类:org.hibernate.dialect.MYSQLDialect

时间:2013-10-06 07:26:00

标签: eclipse hibernate maven exception java-ee

我正在学习Java EE和Hibernate,我遇到了以下问题:

我在Eclipse中创建了一个Dynamic Web Project,我已将其转换为MAVEN项目,将mysql-connector-java和hibernate-core依赖项添加到pom.xml中。 我添加了一个servlet,在doGet方法中,我尝试初始化sessionfactory来创建表。我正在使用tomcat。

当我向servlet发出请求时,我遇到以下异常:

org.hibernate.HibernateException: Dialect class not found:   org.hibernate.dialect.MYSQLDialect
 org.hibernate.service.jdbc.dialect.internal.DialectFactoryImpl.constructDialect(DialectFactoryImpl.java:77)
org.hibernate.service.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:65)
org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:146)
org.hibernate.service.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:76)
org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:160)
org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:132)
org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1818)
org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1776)
hu.adamsan.testhibernate.TestHibernate.doGet(TestHibernate.java:74)

的pom.xml:

dependencies>
    <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.26</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.2.6.Final</version>
        </dependency>
</dependencies>

我试图查看Maven依赖项,org.hibernate.dialect.MySQLDialect.class位于:getServletContext()。getRealPath(“。”) - &gt; E:\ eclips_jee \ workspace .metadata.plugins \ org.eclipse.wst.server.core \ TMP0 \ wtpwebapps \ TestHibernateWeb \ 在WEB-INF \ lib目录中,有hibernate-core-4.2.6.Final.jar,当我用7zip打开它时,我可以在org / hibernate / dialect / MySQL5Dialect.class中找到。

我错过了什么?我真的不知道。

doGet方法:

    PrintWriter w = response.getWriter();
    w.println("Testing hibernate<br/>");
    w.println(getServletContext().getRealPath(".")+"<br/>");

    Configuration config;
    ServiceRegistry registry;
    SessionFactory factory;     
    config = new Configuration().addAnnotatedClass(Modell.class);       
    Properties props = getProperties();     
    props.setProperty(Environment.HBM2DDL_AUTO, "create");      
    config.setProperties(props);        
    registry = new ServiceRegistryBuilder().applySettings(props).buildServiceRegistry();        
    factory = config.buildSessionFactory(registry);     
    factory.close();

1 个答案:

答案 0 :(得分:4)

问题的答案就在于此。您在jar文件中找到了MySQLDialectMySQL5Dialect,但您的配置文件尝试使用MYSQLDialect。 Java区分大小写。