Reputation: 5047
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?
Upvotes: 6
Views: 17058
Reputation: 1
basically dialect work as translator or bridge its convert hibernate queries language (HQL) to connected data queries language . in another word hibernate use dialect to communicate with specific database
Upvotes: 0
Reputation: 14061
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:
Upvotes: 9
Reputation: 31
If we use Dialects in Hibernate any query in the hibernate will be converted into database specific query, regardless of the Database type. And Hibernate supports HQL queries, internally these HQL queries will be converted into native Database SQL calls.
Upvotes: 3