14


In my application, I use Hibernate with SQL Server database, so I set

<property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect">

in my persistence.xml.

In some case, I want to sort records with NULL include, I use keyword NULLS FIRST.
Because it is not supported by default by CriteriaQuery/CriteriaBuilder in Hibernate, then I use Interceptor to modify the native query.
The problem is, keyword NULLS FIRST is not supported in SQL Server, so I use keyword:

case when column_name is null then 0 else 1 end, column_name

If I want to migrate database from SQL Server to Oracle (for example), then I need to put if-else in my Interceptor, choosing which dialect I am using, right?

This is how I illustrate them:

String dialect = ..............
if (dialect.equals("org.hibernate.dialect.SQLServerDialect")) { // get SQL Server dialect
     // put keyword "case when column_name is null then 0 else 1 end, column_name"
} else {
     // put keyword "NULLS FIRST/LAST"
}

How I can get the dialect configuration (in persistence.xml) during runtime?

Neil Stockton
  • 11,383
  • 3
  • 34
  • 29
danisupr4
  • 815
  • 1
  • 9
  • 22
  • another link about NULLS FIRST keyword : https://hibernate.atlassian.net/browse/HHH-465 – danisupr4 Nov 28 '14 at 03:32
  • I use Interceptor based on this article : http://stackoverflow.com/questions/3683174/hibernate-order-by-with-nulls-last – danisupr4 Nov 28 '14 at 03:33
  • These links may help you to get dialect info from SessionFactory; [link1](http://stackoverflow.com/questions/1571928/retrieve-auto-detected-hibernate-dialect) and [link2](http://stackoverflow.com/questions/8742617/resolve-sql-dialect-using-hibernate) – Wundwin Born Nov 28 '14 at 04:36
  • thank you Wundwin Born, I got the solution on the second link : http://stackoverflow.com/questions/8742617/resolve-sql-dialect-using-hibernate – danisupr4 Nov 28 '14 at 07:22

4 Answers4

5

The following works well for me for accessing the dialect in a Java EE application running in WildFly 14:

import org.hibernate.Session;
import org.hibernate.dialect.Dialect;
import org.hibernate.internal.SessionFactoryImpl;

...

@PersistenceContext
private EntityManager entityManager;

...

final Session session = (Session) entityManager.getDelegate();
final SessionFactoryImpl sessionFactory = (SessionFactoryImpl) session.getSessionFactory();
final Dialect dialect = sessionFactory.getJdbcServices().getDialect();
logger.info("Dialect: {}", dialect);

You need to add hibernate-core dependency with provided scope to pom.xml.

mrts
  • 16,697
  • 8
  • 89
  • 72
3

If you use Spring+hibernate, try this

@Autowired@Qualifier("sessionFactory") org.springframework.orm.hibernate3.LocalSessionFactoryBean sessionFactory; //Suppose using hibernate 3

and in your method:

sessionFactory.getHibernateProperties().get("hibernate.dialect")
CE ZHANG
  • 527
  • 4
  • 7
3

I have found the answer from this post : Resolve SQL dialect using hibernate

Thank you for @Dewfy,

here is the solution:

//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().equals("org.hibernate.dialect.SQLServerDialect")) {

} else {

}
Community
  • 1
  • 1
danisupr4
  • 815
  • 1
  • 9
  • 22
  • 6
    This would be quicker and less painful : String dialect = entityManager.getEntityManagerFactory().getProperties().getOrDefault("hibernate.dialect", "").toString(); – Chris March Oct 25 '18 at 16:12
0

The other answers didn't work in my case. This worked:

import java.util.Properties;
import org.hibernate.cfg.Configuration;

Properties properties = new Configuration().configure().getProperties();
String dialect = properties.getProperty("hibernate.dialect");
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
DavidBoe
  • 21
  • 3