Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate: The dialect was not set. Set the property hibernate.dialect

Tags:

java

hibernate

I have the following hibernate.cfg.xml:

<hibernate-configuration>
   <session-factory>
      <property name="hibernate.format_sql">true</property>
      <property name="hibernate.show_sql">true</property>
      <property name="hibernate.connection.url">jdbc:mysql://localhost/EJB</property>
      <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
      <property name="hibernate.connection.username">root</property>
      <property name="hibernate.connection.password">password</property>
      <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
      <property name="hibernate.hbm2ddl.auto">update</property>
      <property name="hibernate.current_session_context_class">org.hibernate.context.ThreadLocalSessionContext</property>
 <!-- Snip -->

Which I consume using this line:

sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();

Then, elsewhere I try this:

SimpleSelect pkSelect = new SimpleSelect(Dialect.getDialect());

Which results in the following exception:

org.hibernate.HibernateException: The dialect was not set. Set the property hibernate.dialect.

Note, the following extract from the logs:

02:26:48,714  INFO Configuration:1426 - configuring from resource: /hibernate.cfg.xml
02:26:48,717  INFO Configuration:1403 - Configuration resource: /hibernate.cfg.xml
02:26:48,909 DEBUG Configuration:1387 - hibernate.dialect=org.hibernate.dialect.MySQLDialect

Any ideas what I'm doing wrong?

like image 729
Marty Pitt Avatar asked Oct 15 '22 03:10

Marty Pitt


1 Answers

The javadoc of getDialect() says:

Get an instance of the dialect specified by the current System properties.

So you have to have hibernate.dialect configured in hibernate.properties for this method to work.

Use SessionFactoryImplementor#getDialect().

like image 72
Bozho Avatar answered Oct 18 '22 13:10

Bozho