tags:

views:

44

answers:

1

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?

+1  A: 

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

Bozho
Thanks, but this isn't really the issue. The config file is being read, and the Dialect is initalized, as indicated in the logs. (I've updated the question with the logs that indicate the file is read)
Marty Pitt
@Matty Pitt see my update
Bozho
Ah. Got it. Thanks
Marty Pitt