Dialect not getting set in hibernate

前提是你 提交于 2019-12-07 09:38:23

问题


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


回答1:


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.




回答2:


I was also facing the same problem Earlier my code was :

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

now I changed it to

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

And the error is gone.I think by default it looks for hibernate.properties file only.




回答3:


The property name is hibernate.dialect

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

I dont know if there is anything like MySQL5Dialect.

Please refer to the documentation for further details.




回答4:


The problem is just in configuring of configuration.

The configure() method of configuration class can get the path of config file of hibernate (hibernate.cfg.xml).

Just tell it the path. I think the problem will solved.




回答5:


In mi case I resolved this by putting jars libs related to hibernate in WEB-INF\lib.




回答6:


I also faced same error- org.hibernate.HibernateException: The dialect was not set. Set the property hibernate.dialect

I forgot to write cfg=cfg.configure(); while creating instance for persistence class.Now its running fine.

Configuration cfg=new Configuration();
              cfg=cfg.configure();
        sfactory=cfg.buildSessionFactory();


来源:https://stackoverflow.com/questions/6818978/dialect-not-getting-set-in-hibernate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!