如何在运行时获得Hibernate方言 [英] How to get Hibernate dialect during runtime
问题描述
在我的应用程序中,我使用Hibernate和SQL Server数据库,所以我设置了
pre code> < property name =hibernate.dialectvalue =org.hibernate.dialect.SQLServerDialect>
在我的persistence.xml中。
在某些情况下,我想用NULL来对记录进行排序include,我使用关键字NULLS FIRST。
由于Hibernate中CriteriaQuery / CriteriaBuilder默认不支持,因此我使用Interceptor修改本机查询
问题是,关键字NULLS FIRST在SQL Server中不受支持,所以我使用关键字:
<$ c $如果我想将数据库从数据库中迁移出来,那么可以将数据库从数据库中移出。 SQL Server到Oracle(例如),那么我需要把if-else放在我的Interceptor中,选择我正在使用哪种方言,对吗?
是我如何说明它们的:字符串方言= .............. $ b $如果(dialect.equals(org.hibernate.dialect.SQLServerDialect)){//获取SQL Server方言
//将关键字case_name当column_name为null然后0 else 1 end,column_name
} else {
//将关键字NULLS FIRST / LAST
}
如何获取方言在运行时配置(在persistence.xml中)?
解决方案如果你使用Spring + hibernate,试试这个
@ Autowired @ Qualifier(sessionFactory)org.springframework.orm.hibernate3.LocalSessionFactoryBean sessionFactory; //假设使用hibernate 3
并在您的方法中:
sessionFactory.getHibernateProperties()。get(hibernate.dialect)
In my application, I use Hibernate with SQL Server database, so I set<property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect">
in my 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: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?
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?
解决方案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")
这篇关于如何在运行时获得Hibernate方言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!