使用hibernate解析SQL方言

时间:2012-01-05 12:39:50

标签: hibernate

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

3 个答案:

答案 0 :(得分:6)

最后我找到了方法 - 但它特定于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")){
   ....

答案 1 :(得分:3)

另一种方式更短:

private @Autowired SessionFactory sessionFactory;

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

答案 2 :(得分:0)

这是针对Hibernate的另一个解决方案。它仍然是丑陋的,因为它涉及向下演员,但它不使用反射:

/Account/Activate/?test=123
相关问题