Java – how to get hibernate dialect at runtime

In my application, I use hibernate and SQL Server database, so I set

<property name="hibernate.dialect" value="org.hibernate.dialect.sqlServerDialect">

In my persistence XML

In some cases, I want to include sort records with null. I use the keyword nulls first Because criteriaquery / criteriabuilder in Hibernate does not support it by default, I use interceptor to modify native queries The problem is that SQL server does not support the keyword nulls first, so I use the keyword:

case when column_name is null then 0 else 1 end,column_name

If I want to migrate the database from SQL server to Oracle (for example), I need to put if else in my interceptor and choose which dialect I am using, right?

That's how I explain 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 do I get dialect configuration at runtime (in persistence. XML)?

Solution

If you use spring hibernate, try this

@Autowired@Qualifier("sessionFactory") org.springframework.orm.hibernate3.LocalSessionfactorybean sessionFactory; //Suppose using hibernate 3

In your approach:

sessionFactory.getHibernateProperties().get("hibernate.dialect")
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>