java 如何在运行时获取 Hibernate 方言
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27181397/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get Hibernate dialect during runtime
提问by danisupr4
In my application, I use Hibernate with SQL Server database, so I set
在我的应用程序中,我将 Hibernate 与 SQL Server 数据库一起使用,因此我设置了
<property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect">
in my persistence.xml.
在我的persistence.xml 中。
In some case, I want to sort records with NULL include, I use keyword NULLS FIRST.
Because it is not supported by default by CriteriaQuery/CriteriaBuilder in Hibernate, then I use Interceptor to modify the native query.
The problem is, keyword NULLS FIRST is not supported in SQL Server, so I use keyword:
在某些情况下,我想对包含 NULL 的记录进行排序,我使用关键字 NULLS FIRST。
因为Hibernate中CriteriaQuery/CriteriaBuilder默认不支持,所以我用Interceptor来修改原生查询。
问题是,SQL Server 不支持关键字 NULLS FIRST,所以我使用关键字:
case when column_name is null then 0 else 1 end, column_name
If I want to migrate database from SQL Server to Oracle (for example), then I need to put if-else in my Interceptor, choosing which dialect I am using, right?
如果我想将数据库从 SQL Server 迁移到 Oracle(例如),那么我需要将 if-else 放在我的拦截器中,选择我使用的方言,对吗?
This is how I illustrate them:
我是这样说明它们的:
String dialect = ..............
if (dialect.equals("org.hibernate.dialect.SQLServerDialect")) { // get SQL Server dialect
// put keyword "case when column_name is null then 0 else 1 end, column_name"
} else {
// put keyword "NULLS FIRST/LAST"
}
How I can get the dialect configuration (in persistence.xml) during runtime?
如何在运行时获取方言配置(在persistence.xml 中)?
回答by CE ZHANG
If you use Spring+hibernate, try this
如果你使用 Spring+hibernate,试试这个
@Autowired@Qualifier("sessionFactory") org.springframework.orm.hibernate3.LocalSessionFactoryBean sessionFactory; //Suppose using hibernate 3
and in your method:
并在您的方法中:
sessionFactory.getHibernateProperties().get("hibernate.dialect")
回答by mrts
The following works well for me for accessing the dialect in a Java EE application running in WildFly 14:
以下内容非常适合我访问在 WildFly 14 中运行的 Java EE 应用程序中的方言:
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 providedscope to pom.xml
.
您需要将hibernate-core
具有提供范围的依赖项添加到pom.xml
.
回答by danisupr4
I have found the answer from this post : Resolve SQL dialect using hibernate
我从这篇文章中找到了答案:Resolve SQL dialect using hibernate
Thank you for @Dewfy,
谢谢@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 {
}