Allow SQL dialect using sleep mode

I am responsible for porting an existing project from Oracle to MSSQL, but keeping both functional. The Legacy project uses Hibernate 3.x, but contains a number of complex source queries. Therefore, I would like to know which dialect is used.

+4
source share
3 answers

I finally found a way, but it is specific to Hibernate.

//take from current EntityManager current DB Session Session session = (Session) em.getDelegate(); //Hibernate 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")){ .... 
+5
source

Another way is a bit shorter:

 private @Autowired SessionFactory sessionFactory; public Dialect getDialecT(){ SessionFactoryImplementor sessionFactoryImpl = (SessionFactoryImplementor) sessionFactory; return sessionFactoryImpl.getDialect(); } 
+2
source

Here's another solution specific to Hibernate. It is still ugly because it includes dropping, but it does not use reflection:

 Session session = (Session) entityManager.getDelegate(); SessionFactoryImplementor sessionFactory = (SessionFactoryImplementor) session.getSessionFactory(); Dialect dialect = sessionFactory.getDialect(); if (dialect.toString().contains("Oracle")) { ... } 
0
source

Source: https://habr.com/ru/post/1207859/


All Articles