How to get Hibernate dialect during runtime

前端 未结 3 1614
醉酒成梦
醉酒成梦 2021-01-17 14:14


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



        
相关标签:
3条回答
  • 2021-01-17 14:54

    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 {
    
    }
    
    0 讨论(0)
  • 2021-01-17 14:59

    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")
    
    0 讨论(0)
  • 2021-01-17 14:59

    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.

    0 讨论(0)
提交回复
热议问题