使用hibernate解析SQL方言

Dew*_*wfy 5 hibernate

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

Dew*_*wfy 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")){
   ....
Run Code Online (Sandbox Code Playgroud)


bor*_*jab 5

另一种稍微短一点的方法:

private @Autowired SessionFactory sessionFactory;

public Dialect getDialecT(){
    SessionFactoryImplementor sessionFactoryImpl = (SessionFactoryImplementor) sessionFactory;      
    return sessionFactoryImpl.getDialect();     
}
Run Code Online (Sandbox Code Playgroud)