方言没有在休眠状态下设置 [英] Dialect not getting set in hibernate

查看:103
本文介绍了方言没有在休眠状态下设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Hibernate 3和MySQL5.5。

我是hibernate的新手,我得到了下面的异常

 线程main中的异常org.hibernate.HibernateException:'hibernate.dialect'必须在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 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。类); 
sessionFactory = cfg.buildSessionFactory();

以下是实际的配置文件

 <?xml version ='1.0'encoding ='utf-8'?> 
<!DOCTYPE hibernate-configuration PUBLIC
- // Hibernate / Hibernate配置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>建立< / property>
<! - JDBC连接池(使用内置) - >
< property name =connection.pool_size> 1< / property>
< property name =current_session_context_class>线程< / property>
<! - 映射文件将在此处显示.... - >
< / session-factory>
< / hibernate-configuration>

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


解决方案

你的第一行或第二行应该工作。第二种方式,只用方言就是 Hibernate参考资料。如果您仍然收到该错误,则表示您可能还有其他事情正在进行。你建立SessionFactory吗?你确定它找到你的hibernate.cfg.xml吗?



编辑:根据你的更新,你不是告诉Hibernate从配置文件中配置自己,你正在构建自己的配置并使用它,你需要选择其中一个或者另一个,或者执行亲语法,或者通过XML


I am using Hibernate 3 and MySQL5.5.

I am a newbie to hibernate and I am getting the below Exception

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)

I have set the Dialect property in hibernate.cfg.xml file. I tried a lot of combinations

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

What is the actual property name ? Hibernate.dialect or only dialect ? What can be possible property values ?

I am adding some more information, I used

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

as suggested by below answers.

I am not even building any code just trying to create simple configuration:

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

Below is the actual Configuration file

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

I suspect the hibernate.cfg.xml is not being found ? I have placed it in the same package where the source code for Employee.class is there. Infact moving around this file causes the same error, so it is not actually found :-( Where to keep it ? This is a standalone Test program :-(

解决方案

Either your first or second line should work. The second way, with just "dialect" is the way shown in the Hibernate reference. If you're still getting that error, it suggests you may have something else going on. How are you building the SessionFactory? Are you sure it's finding your hibernate.cfg.xml?

Edit: Based on your update, you aren't telling Hibernate to configure itself from the config file. You're building your own Configuration and using that. You need to pick one or the other. Either do it programmatically, or do it via XML.

这篇关于方言没有在休眠状态下设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆