2 Replies Latest reply on Sep 29, 2008 12:26 PM by shelly.mcgowan

    Can you omit hibernate.dialect in persistence.xml?

    dweil

      We want to configure the persistence layer of our application for different databases, i.e. the same application ear file should be able to be run on a JBoss with MySQL as database and on another with DB2.

      Of course the JNDI name of the datasources are the same ;-)

      The problem now is that the hibernate dialect has to be specified in persistence.xml, e.g.

      <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>

      or
      <property name="hibernate.dialect" value="org.hibernate.dialect.DB2Dialect"/>


      In order to make the application as portable as possible, it would be necessary to omit this property from persistence.xml. Is that possible? Can Hibernate figure out the db dialect by itself? Or can the dialect be specified somewhere in the server configuration?

      Thanks in advance!
      Dirk

        • 1. Re: Can you omit hibernate.dialect in persistence.xml?
          peterj

          Not sure if this will work but it is worth a try. Set the property using a system property like this:

          <property name="hibernate.dialect" value="org.hibernate.dialect.${my.hibernate.dialect:MySQL5}Dialect"/>


          Then when you run the app server, set -Dmy.hibernate.dialect=DB2 on the command line to use DB2. If this option is missing, the above line uses he MySQL dialect by default.

          The above makes use of the ability of JBoss AS to replace system properties within configuration file. I am not sure that it will work for the Hibernate config files.

          And I have another thought - perhaps you can override the dialect by setting -Dhibernate.dialect on the command line. (Not sure if this works either, I have not looked this up in the docs nor looked at the code to see if it might work but a grep for "hibernate.dialect" in the source code should verify if this is possible.)

          • 2. Re: Can you omit hibernate.dialect in persistence.xml?
            shelly.mcgowan

            The dialect should be automatically detected. Or it can be set explicitly in the $JBOSS_HOME/server/config/deploy/ejb3.deployer/META-INF/persistence.properties. Auto-detection for DB2 was recently added so depends on the Hibernate version you are currently using. See:
            http://opensource.atlassian.com/projects/hibernate/browse/HHH-2630