Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resolve SQL dialect using hibernate

Tags:

hibernate

I'm responsible on porting existing project from Oracle to MSSQL, but keeping both functional. Legacy project uses Hibernate 3.x, but contains some number of sophisticated native queries. So I would like to know which dialect used.

like image 250
Dewfy Avatar asked Jan 05 '12 12:01

Dewfy


People also ask

What is SQL dialect in hibernate?

Dialect is a class that acts as a bridge between Java JDBC types and SQL types, which contains the mapping between java language data type and database datatype. Dialect allows Hibernate to generate SQL optimized for a particular relational database.

What is the use of dialect property in hibernate?

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.

How do I add Hibernate dialect?

configure(). buildSessionFactory() : For your all of entities, Hibernate generates a native query for create tables and executes it using JDBC. In order to generate native query, Hibernate uses your entity mapping and uses the provided dialect class to map java types to database data types.

What is database dialect?

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.


1 Answers

At last I've found the way - but it is specific for Hibernate.

//take from current EntityManager current DB Session
Session session = (Session) em.getDelegate();
//Hibernate's SessionFactoryImpl has property 'getDialect', to
//access this I'm using property accessor:
Object dialect = 
       org.apache.commons.beanutils.PropertyUtils.getProperty(
          session.getSessionFactory(), "dialect");
//now this object can be casted to readable string:
if( dialect.toString().contains("Oracle")){
   ....
like image 131
Dewfy Avatar answered Dec 17 '22 22:12

Dewfy