1

I have a hibernate.cfg.xml file, but I'd like to reference it as a dataSource bean. Is there any way to do this? All I have is :

<beans:bean id="foo" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <beans:property name="configLocation" value="classpath:hibernate.cfg.xml"/>

but its not a dataSource. Any help would be greatly appreciated.

To add a bit of context, I'd like to integrate Spring Security in my app and one of the ways to provide a database as a source for the authentication manager is as follows (taken from here):

   <authentication-manager>
        <authentication-provider>
            <jdbc-user-service data-source-ref="securityDataSource"/>
        </authentication-provider>
    </authentication-manager>

Where “securityDataSource” is the name of a DataSource bean in the application context, pointing at a database.

3 Answers 3

1

You have two options:

  1. If you're not using the hibernate.cfg.xml anywhere else, you can eliminate it altogether and just use Spring's configuration (see below)

  2. If you need hibernate.cfg.xml for some reason (you're using it elsewhere), then keep that file and add Spring configuration


Example configuration (notice database access is in a separate dataSource bean)

<beans:bean id="foo" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
       <property name="hibernateProperties">
            <props>
                <prop key="hibernate.hbm2ddl.auto">create-drop</prop>
                ...
        </property>
        <property name="packagesToScan">
            <list>
                <value>some.package</value>
            </list>
        </property>
    <property name="dataSource" ref="c3p0DataSource" />

<bean id="c3p0DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
    scope="singleton"
        destroy-method="close">
        <property name="driverClass">
            <value>org.postgresql.Driver</value>
        </property>
        <property name="jdbcUrl">
            <value>${jdbc.url}</value>
        </property>
        <property name="user">
            <value>${jdbc.user}</value>
        </property>
        <property name="password">
            <value>${jdbc.pw}</value>
        </property>
    </bean>

You can see more information about the configuration options in http://static.springsource.org/spring/docs/current/spring-framework-reference/html/orm.html

Sign up to request clarification or add additional context in comments.

Comments

0

if you are using hibernate than why to use jdbc you can use spring bean for this like this

<security:authentication-manager>
<security:authentication-provider ref="myAuthenticationProvider" />
</security:authentication-manager>
<bean id="myAuthenticationProvider" class="com.something.MyAuthenticationProvider"/>

where MyAuthenticationProvider is Spring bean that implements AuthenticationProvider. you can also checkout following link for more help Link1 and Link2

Comments

0

You can create directly from spring no need of cfg.xml file.

dialect 50 false false false You can configure all Hibernate properties here...

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.