使用hibernate解析SQL方言

使用hibernate解析SQL方言,hibernate,Hibernate,我负责将现有项目从Oracle移植到MSSQL,但保持两者的功能。 遗留项目使用Hibernate3.x,但包含一些复杂的本机查询。 所以我想知道我用的是哪种方言。我终于找到了方法——但这是针对Hibernate的 //take from current EntityManager current DB Session Session session = (Session) em.getDelegate(); //Hibernate's SessionFactoryImpl has proper

我负责将现有项目从Oracle移植到MSSQL,但保持两者的功能。 遗留项目使用Hibernate3.x,但包含一些复杂的本机查询。
所以我想知道我用的是哪种方言。

我终于找到了方法——但这是针对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")){
   ....

下面是另一个针对Hibernate的解决方案。它仍然很难看,因为它涉及向下投射,但它不使用反射:

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

另一种方式稍微短一点:

private @Autowired SessionFactory sessionFactory;

public Dialect getDialecT(){
    SessionFactoryImplementor sessionFactoryImpl = (SessionFactoryImplementor) sessionFactory;      
    return sessionFactoryImpl.getDialect();     
}