Dialect not getting set in hibernate

Geek picture Geek · Jul 25, 2011 · Viewed 54.9k times · Source

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

Answer

Ryan Stewart picture Ryan Stewart · Jul 25, 2011

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.