方言没有进入休眠状态

时间:2011-07-25 16:01:06

标签: hibernate dialect

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

我是hibernate的新手,我收到以下异常

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的源代码就在那里。实际上移动这个文件导致相同的错误,所以实际上找不到:-(在哪里保留它?这是一个独立的测试程序: - (

6 个答案:

答案 0 :(得分:6)

您的第一行或第二行应该有效。第二种方式,仅使用“方言”就是Hibernate reference中显示的方式。如果您仍然收到该错误,则表示您可能还有其他问题。你是如何构建SessionFactory的?你确定它找到你的hibernate.cfg.xml吗?

编辑:根据您的更新,您不会告诉Hibernate从配置文件中进行配置。您正在构建自己的配置并使用它。你需要挑选一个或另一个。要么programmatically,要么via XML

答案 1 :(得分:5)

我也遇到了同样的问题 早些时候,我的代码是:

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

现在我将其更改为

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

错误消失。我认为默认它只查找hibernate.properties文件。

答案 2 :(得分:2)

属性名称为 hibernate.dialect

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

我不知道是否有类似 MySQL5Dialect 的内容。

有关详细信息,请参阅documentation

答案 3 :(得分:0)

问题仅在于配置配置。

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

告诉它路径。我认为问题会解决。

答案 4 :(得分:0)

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

答案 5 :(得分:0)

我也遇到了同样的错误 - org.hibernate.HibernateException:没有设置方言。设置属性hibernate.dialect

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

Configuration cfg=new Configuration();
              cfg=cfg.configure();
        sfactory=cfg.buildSessionFactory();
相关问题