5

Little confused, is 'driverclassname' and 'hibernate.dialect' both referring to the mysql driver?

What should I be using? Is the connectorJ the one I should use?

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
        <property name="url" value="jdbc:mysql://localhost/blah"/>



<property name="hibernateProperties">
            <value>
                hibernate.dialect=org.hibernate.dialect.MySQLDialect

I'm using Maven so if I can get the driver from maven that would be ideal.

Running my app in tomcat I get the error:

Cannot create JDBC driver of class 'org.hsqldb.jdbcDriver' for connect URL 
Blankman
  • 259,732
  • 324
  • 769
  • 1,199
  • 1
    Is it me or did you originally had `com.mysql.jdbc.Driver` in the `driverClassName` and did you quickly edit it within the 5 minute limit? It makes my answer look a bit dumb... – BalusC Jul 19 '10 at 02:47
  • yes I did change it, I had hsqldb and was trying out mysql but wasn't sure if I was on the right track.... – Blankman Jul 19 '10 at 03:13
  • 1
    Seriously, I don't understand the accepted answer, it just does not answer the body of your question. If what you were looking for was some maven coordinates, then **write it** and tag your question appropriately. First, it would allow readers like @BalusC or me to not waste their time answering something you don't care about. Second, in its current state, this question/answer makes little sense and what you're currently doing is a poor practice. Sure, you got an answer to a problem that you didn't even express. But the counterpart is that you are making SO less relevant. This is not good. – Pascal Thivent Jul 19 '10 at 04:17

3 Answers3

24

Little confused, is 'driverclassname' and 'hibernate.dialect' both referring to the mysql driver?

No, they are not. The driverclassname is referring to, well, the driver class name which is the class from a given JDBC driver that implements java.sql.Driver. The driver class name is driver specific.

When using MySQL's JDBC driver aka MySQL Connector/J, this class is com.mysql.jdbc.Driver as explained in the MySQL Connector/J documentation:

20.3.4.1. Driver/Datasource Class Names, URL Syntax and Configuration Properties for Connector/J

The name of the class that implements java.sql.Driver in MySQL Connector/J is com.mysql.jdbc.Driver. (...)

And actually, they even provide instructions to use their driver with Spring. See the section 20.3.5.2.4. Using Connector/J with Spring.

The hibernate.dialect is different, this configuration property is used to define the classname of a Hibernate org.hibernate.dialect.Dialect which allows Hibernate to generate SQL optimized for a particular relational database. Again this is explained in the Hibernate documentation:

3.4. Optional configuration properties

(...) The classname of a Hibernate org.hibernate.dialect.Dialect which allows Hibernate to generate SQL optimized for a particular relational database.

e.g. full.classname.of.Dialect

In most cases Hibernate will actually be able to choose the correct org.hibernate.dialect.Dialect implementation based on the JDBC metadata returned by the JDBC driver.

For MySQL 5.x, you should use org.hibernate.dialect.MySQL5InnoDBDialect if you are using InnoDB tables (this would be my recommendation) or org.hibernate.dialect.MySQL5Dialect if you're not. See the section 3.4.1. SQL Dialects for a (non exhaustive) list.

Last point, the Maven part that you didn't even mention in your question... The MySQL JDBC driver is available in the Maven central repository and you should use a repository search engine (as I already suggested). For example, the following query:

http://www.jarvana.com/jarvana/search?search_type=project&project=mysql

allows to find the maven coordinates of the ultimate version in two clicks:

<dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
   <version>5.1.13</version>
</dependency>

PS: I don't mean to be rude and I'm glad to help but you should really try to leverage the documentation of the products or frameworks you're using. What you're asking in this question is well documented (as I showed) and can be found easily. Learning to find basic information by yourself is a fundamental skill for a software developer in my opinion.

Community
  • 1
  • 1
Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
1

regarding maven mysql definition, here's one that seems to work.

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.12</version>
</dependency>
tim_wonil
  • 14,970
  • 6
  • 29
  • 42
0

The driverClassName should be referring to the class name of the JDBC driver you'd like to load (as you usually would do using Class#forName() in "plain" JDBC). The one you're currently specifying is correct for the MySQL JDBC driver (Update: you quickly edited it into a HSQLDB JDBC driver one, this is only correct for a Hypersonic DB, not for a MySQL DB).

The hibernate.dialect should be referring to the class name of the Hibernate Dialect implementation you'd like to use for the particular database, so that Hibernate knows what the DB in question understands so that it can autogenerate the suitable SQL statements. The one you're currently specifying is correct for the MySQL database.


That said, it sounds like that you're having problems with it. Probably you haven't installed the MySQL JDBC driver? Are you getting a ClassNotFoundException on it? Just downloading Connector/J, extracting the zip and placing the JAR file in the runtime classpath ought to be sufficient.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • so I had org.hsqldb.jdbcDriver, it should be com.mysql.jdbc.Driver? I can't find the mysql.jdbc in maven, any tips? – Blankman Jul 19 '10 at 02:42
  • If you're using Hypersonic DB, then you should put its JDBC driver in the classpath, alter the `driverClassName` to reflect the HSQLDB JDBC driver class name so that the conneciton pool knows what JDBC driver it should load and the `hibernate.dialect` to reflect the HSQLDB dialect so that Hibernate knows what DB it is talking to. – BalusC Jul 19 '10 at 02:44
  • I'm however tempted to delete this answer since the OP don't seem to appreciate my effort or even the newly learnt things(!) in any way. – BalusC Jul 19 '10 at 13:27