I'm trying to configure a Spring+Hibernate web application using JNDI instead of a properties files. Everything works fine por database url, password, login, etc., but I can't get the dialect from the JNDI context.
I'm using this in my datatabase-context.xml:
<bean id="databaseUrl" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/url"/>
</bean>
<bean id="databaseDriver" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/driver"/>
</bean>
<bean id="databaseUsername" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/username"/>
</bean>
<bean id="databasePassword" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/password"/>
</bean>
<bean id="databaseDialect" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/dialect"/>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" ref="databaseDriver"/>
<property name="url" ref="databaseUrl"/>
<property name="username" ref=databaseUsername"/>
<property name="password" ref="databasePassword"/>
<property name="maxWait" value="30000"/>
<property name="validationQuery" value="select 1"/>
<property name="maxActive" value="100"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan">...</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
<!--<prop key="hibernate.dialect">org.hibernate.dialect.DerbyDialect</prop>-->
<!--<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>-->
</props>
</property>
</bean>
The problem is that I don't know how to use the JNDI value for databaseDialect inside the <prop name="hibernate.dialect">blabla</prop>
property.
Any ideas?
Spring can convert a Map
to a Properties
object, so you can do the following:
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<util:map>
<entry key="hibernate.hbm2ddl.auto" value="update" />
<entry key="hibernate.show_sql" value="true" />
<entry key="hibernate.dialect">
<jee:jndi-lookup jndi-name="java:comp/env/jdbc/dialect" />
</entry>
</util:map>
</property>
</bean>
Reference:
Handles conversion from content
String
toProperties
object. Also handlesMap
toProperties
conversion, for populating aProperties
object via XML "map" entries.
Source: PropertiesEditor
javadoc