Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dialect in Hibernate

Tags:

hibernate

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

For Example, suppose we are using MySQL database, then we need below dialect: org.hibernate.dialect.MySQLDialect

Suppose we are using SQL Server database, then we need below dialect: org.hibernate.dialect.SQLServerDialect

Hibernate will generate appropriate query related to that database. My question is that Which mechanism used by hibernate to generate query based on database?

like image 950
kandarp Avatar asked Dec 16 '10 04:12

kandarp


People also ask

What is dialect in database?

A database dialect is a configuration setting for platform independent software (JPA, Hibernate, etc) which allows such software to translate its generic SQL statements into vendor specific DDL, DML.

Is dialect mandatory in hibernate?

For connecting any hibernate application with the database, it is required to provide the configuration of SQL dialect.

What is the purpose of Hibernate dialect property in Hibernate configuration?

Hibernate uses "dialect" configuration to know which database you are using so that it can switch to the database specific SQL generator code wherever/whenever necessary.

What is Spring JPA properties Hibernate dialect?

It is the default JPA vendor that comes with spring-data-jpa. Although Hibernate is database agnostic, we can specify the current database dialect to let it generate better SQL queries for that database. The ddl-auto property is used to automatically create the tables based on the entity classes in the application.


1 Answers

Not sure I understood your question, but I'll try :-)

First, there are some things which works in all databases, while other things are specific for some databases (like "what's the current date and time?"). For things which works in all databases, there's nothing really specific to the dialects.

But for all parts which may be different from a database to another, Hibernate uses the Dialect. Dialect is a "helper" for Hibernate to communicate with the database in its language. For instance, at some point, a Hibernate code needs to know what's the database data type for the JDBC type "Types.TIMESTAMP". The "core" code of Hibernate just says "give me whatever is this db's equivalent of Types.TIMESTAMP", and the specific Dialect answers to that.

The same happens for SQL generation (or query generation, as you asked). Hibernate knows the basic structure, but it also provides some hooks in the Dialect so that a specific Dialect can say "I don't support feature X", or, "for feature Y, use the string 'abc' in the query".

Of course, my answer is an extreme simplification of the whole process. I recommend reading the code for Dialect.java and, for instance, MySQLDialect.java. This way, you can have an idea on how Hibernate structures (and even how it consumes) some of this information:

https://github.com/hibernate/hibernate-orm/blob/master/hibernate-core/src/main/java/org/hibernate/dialect/Dialect.java

https://github.com/hibernate/hibernate-orm/blob/master/hibernate-core/src/main/java/org/hibernate/dialect/MySQLDialect.java

like image 90
jpkrohling Avatar answered Sep 19 '22 22:09

jpkrohling