Snehal Masne
Snehal Masne

Reputation: 3429

Why doesn't Hibernate figure out the 'Dialect' from the specified DB driver?

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?

Upvotes: 1

Views: 580

Answers (2)

Learner
Learner

Reputation: 21445

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.

Upvotes: 2

ajay.patel
ajay.patel

Reputation: 1967

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?

Upvotes: 1

Related Questions