7

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

3
  • We're gonna need a precise Hibernate version. "3" means anything starting from ~2007... The dialect property value has to be any class from the package org.hibernate.dialect, you can find a list of possible ones in the relevant javadoc for your version. Also, the hibernate.cfg.xml should be in your classpath, which could possibly mean a bunch of places, so we're also gonna need to know the build system you're using, i.e., for Maven it would be proj/src/main/resources
    – TC1
    Commented Jul 25, 2011 at 16:57
  • Hi, I am running a simple public static void main() from Eclipse.
    – Geek
    Commented Jul 25, 2011 at 17:02
  • 1
    Well 3.6 has MySQL5Dialect, so you should probably be using that, although if you use the InnoDB engine, there's a dialect for that too. Sorry, "simple main()" won't say much right now. The best answer I can give you is that the file needs to be in your classpath. You can try calling the configure(File path) method on your Configuraion object with the full path & see what happens...
    – TC1
    Commented Jul 25, 2011 at 17:02

6 Answers 6

7

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.

1
  • Thanks Ryan, it seems the .cfg file is not being found :-( I have added more details in the question.
    – Geek
    Commented Jul 25, 2011 at 16:55
5

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.

2

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.

2
0

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.

0

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

0

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();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.