2014-11-28 13 views
5


Nella mia domanda, io uso Hibernate con il database SQL Server, così mi sono messoCome arrivare Hibernate dialetto durante il runtime

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

nel mio persistence.xml.

In alcuni casi, desidero ordinare i record con NULL include, utilizzo la parola chiave NULLS FIRST.
Poiché non è supportato per impostazione predefinita da CriteriaQuery/CriteriaBuilder in Hibernate, quindi utilizzo Interceptor per modificare la query nativa.
Il problema è, NULLS parola chiave PRIMO non è supportato in SQL Server, in modo che uso per parola chiave:

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

Se voglio migrare database da SQL Server a Oracle (ad esempio), quindi ho bisogno di mettere se -seleziona nel mio Interceptor, scegliendo quale dialetto sto usando, giusto?

Questo è come io li illustro:

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" 
} 

Come posso ottenere la configurazione dialetto (in persistence.xml) durante il runtime?

+0

un altro collegamento su NULLS prima parola chiave: https://hibernate.atlassian.net/browse/HHH-465 – danisupr4

+0

Io uso Interceptor in base a questo articolo: http://stackoverflow.com/questions/3683174/hibernate- order-by-with-nulls-last – danisupr4

+0

Questi collegamenti possono aiutarti a ottenere informazioni su dialect da SessionFactory; [link1] (http://stackoverflow.com/questions/1571928/retrieve-auto-detected-hibernate-dialect) e [link2] (http://stackoverflow.com/questions/8742617/resolve-sql-dialect-using -ibernativo) –

risposta

0

Se si utilizza Primavera + ibernazione, provate questo

@[email protected]("sessionFactory") org.springframework.orm.hibernate3.LocalSessionFactoryBean sessionFactory; //Suppose using hibernate 3 

e nel metodo:

sessionFactory.getHibernateProperties().get("hibernate.dialect") 
0

ho trovato la risposta da questo post: Resolve SQL dialect using hibernate

Grazie per @ Dewfy,

ecco la soluzione:

//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 { 

} 
Problemi correlati