6

我正在使用 Hibernate 3 和 MySQL5.5。

我是休眠的新手,我得到以下异常

Exception in thread "main" org.hibernate.HibernateException: 'hibernate.dialect' must be set when no Connection available
    at org.hibernate.dialect.resolver.DialectFactory.buildDialect(DialectFactory.java:106)
    at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:152)

我在 hibernate.cfg.xml 文件中设置了 Dialect 属性。我尝试了很多组合

<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> 
<property name="DIALECT">org.hibernate.dialect.MySQL5Dialect</property>

实际的属性名称是什么?Hibernate.dialect 还是只有方言?什么是可能的属性值?

我正在添加更多信息,我使用了

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

正如以下答案所建议的那样。

我什至没有构建任何代码,只是试图创建简单的配置:

Configuration cfg = new Configuration().addClass(Employee.class);
sessionFactory = cfg.buildSessionFactory();

下面是实际的配置文件

<?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>

    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="connection.url">jdbc:mysql://localhost/test</property>
    <property name="connection.username">root</property>
    <property name="connection.password">root1234</property>
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="show_sql">true</property>
    <property name="format_sql">true</property>
    <property name="hbm2ddl.auto">create</property>
    <!-- JDBC connection pool (use the built-in) -->
    <property name="connection.pool_size">1</property>
    <property name="current_session_context_class">thread</property>
    <!-- Mapping files will go here.... -->
  </session-factory>
</hibernate-configuration>

我怀疑没有找到hibernate.cfg.xml?我已将它放在 Employee.class 的源代码所在的同一个包中。事实上,在这个文件中移动会导致同样的错误,所以实际上并没有找到它:-(在哪里保存它?这是一个独立的测试程序:-(

4

6 回答 6

7

您的第一行或第二行应该可以工作。第二种方式,仅使用“方言”是Hibernate 参考中显示的方式。如果您仍然收到该错误,则表明您可能还有其他事情发生。你是如何构建 SessionFactory 的?你确定它正在找到你的 hibernate.cfg.xml 吗?

编辑:根据您的更新,您并没有告诉 Hibernate 从配置文件中配置自己。您正在构建自己的配置并使用它。你需要选择一个或另一个。要么以编程方式完成,要么通过 XML完成。

于 2011-07-25T16:12:44.050 回答
5

我也面临同样的问题早些时候我的代码是:

private final static SessionFactory factory = new Configuration()
        .configure().buildSessionFactory();

现在我把它改成了

private final static SessionFactory factory = new Configuration()
        .configure("hibernate.cfg.xml").buildSessionFactory();

并且错误消失了。我认为默认情况下它只查找 hibernate.properties 文件。

于 2012-02-08T13:55:17.633 回答
2

属性名称是hibernate.dialect

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

我不知道是否有类似MySQL5Dialect的东西。

请参阅文档以获取更多详细信息。

于 2011-07-25T16:16:10.040 回答
0

问题只是在配置配置中。

配置类的configure()方法可以获取配置文件的路径hibernate (hibernate.cfg.xml)

只告诉它路径。我想问题会解决的。

于 2014-02-08T13:16:26.113 回答
0

在我的情况下,我通过将与休眠相关的 jar 库放在 WEB-INF\lib 中来解决这个问题。

于 2014-02-11T23:39:20.033 回答
0

我也遇到了同样的错误——org.hibernate.HibernateException: The dialect was not set。设置属性 hibernate.dialect

我忘了写 cfg=cfg.configure(); 在为持久性类创建实例时。现在它运行良好。

Configuration cfg=new Configuration();
              cfg=cfg.configure();
        sfactory=cfg.buildSessionFactory();
于 2015-12-09T00:19:55.740 回答