使用 JNDI 配置 hibernate 方言(使用 Spring)

发布于 2024-10-12 07:03:37 字数 2397 浏览 7 评论 0原文

我正在尝试使用 JNDI 而不是属性文件来配置 Spring+Hibernate Web 应用程序。数据库 url、密码、登录名等一切工作正常,但我无法从 JNDI 上下文获取方言。

我在我的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>

问题是我不知道如何在 blabla 属性。

有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

久随 2024-10-19 07:03:37

Spring 可以将 Map 转换为 Properties 对象,因此您可以执行以下操作:

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

参考:

处理内容字符串的转换
Properties 对象。还处理地图
Properties 转换,对于
通过 XML 填充 Properties 对象
“地图”条目。

来源:PropertiesEditor javadoc

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
to Properties object. Also handles Map
to Properties conversion, for
populating a Properties object via XML
"map" entries.

Source: PropertiesEditor javadoc

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文