1

I am newbie to Hibernate, might be missing something basic.

Hibernate is database independent. So, whatever database we will use in our application, we need to set dialect related to that database.

But why do we need that? Can't Hibernate figure that out by the type of DB driver we specified already?

1
  • Often times the driver and connection string are using a proxy to the real driver. Commented Sep 8, 2014 at 12:18

2 Answers 2

2

Hibernate first checks for the dialect property in configuration file, if the property is missing then it uses the DB connection details and tries to load the default dialect that is suitable for your Database.

See this link for DialectFactory, it says:

Builds an appropriate Dialect instance. If a dialect is explicitly named in the incoming properties, it should used. Otherwise, it is determined by dialect resolvers based on the passed connection.

An exception is thrown if a dialect was not explicitly set and no resolver could make the determination from the given connection.

And also here for Optional configuration properties, it says:

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.

This link for SQL Dialects says, if you specify the dialect then hibernate tries to add some default values to some other properties based on given dialect:

Always set the hibernate.dialect property to the correct org.hibernate.dialect.Dialect subclass for your database. If you specify a dialect, Hibernate will use sensible defaults for some of the other properties listed above. This means that you will not have to specify them manually.

So finally it is recommended to specify the dialect, but even if you do not provide the details then hibernate tries to use the default dialect.

Sign up to request clarification or add additional context in comments.

Comments

1

As much as I understand you are somewhat confused with Driver and Dialect. There are two different concept all together.

You need Database driver for stuff like connecting to the database and executing the queries and get the result. But since hibernate is a ORM framework, we have another layer here, hibernate first needs to convert those operations like save(Object) into a query that driver can understand and execute. So dialect basically is the grammar that hibernate uses for these conversions.

Moreover, its better to keep it decoupled. Why would you want to be dependent on a driver class(indirectly on driver provider), rather let the user of the framework choose between the available dialect. Does that make sense?

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.