简体   繁体   English

在Spring应用程序中配置休眠

[英]Configure hibernate in spring application

I have successfully configured hibernate and I can run transactions but only from the psvm of the DAO class. 我已经成功配置了休眠模式,并且只能从DAO类的psvm运行事务。 I want to configure it with my spring app using the same configuration file ie hibernate.cfg.xml . 我想使用相同的配置文件(即hibernate.cfg.xml在spring app中对其进行配置。

How can I do this? 我怎样才能做到这一点? Most tutorials I've read simply neglect the hibernate configuration file. 我读过的大多数教程都只是忽略了休眠配置文件。

You can add this code to you xml file to configure hibernate. 您可以将此代码添加到xml文件中以配置休眠状态。

<!-- Hibernate Related Configuration. -->

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="org.postgresql.Driver"/>
        <property name="url" value="jdbc:postgresql://192.168.1.9:5432/dbname"/>
        <property name="username" value="postgres"/>
        <property name="password" value="pwd"/>
        <property name="validationQuery" value="SELECT 1"/>
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="packagesToScan" value="com.domain"/>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.generate_statistics">true</prop>
            </props>
        </property>
    </bean>

    <!-- Transaction Manager -->
    <bean id="txManager"
          class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <tx:annotation-driven transaction-manager="txManager" />

The hibernate.cfg.xml file is specified for the LocalEntityManagerFactoryBean, along with your DataSource 为LocalEntityManagerFactoryBean和您的数据源指定了hibernate.cfg.xml文件

<bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceXmlLocation" value="classpath*:META-INF/hibernate.cfg.xml" />
        <property name="dataSource" ref="dataSource" />
    </bean>

Here you can find an example of a Spring XML configuration containing some Hibernate configuration 在这里,您可以找到一个包含一些Hibernate配置的Spring XML配置示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM