【问题标题】:Spring boot 2 application context xml hibernate propertiesSpring Boot 2 应用程序上下文 xml 休眠属性
【发布时间】:2020-06-08 16:25:48
【问题描述】:

我正在将旧的 Spring 应用程序转换为 Spring boot。所以我仍将使用现有的 xml 配置而不是注释。在 appContext.xml 中,我配置了要从应用程序属性中读取的休眠属性,如下所示。

<bean id="appSessionFactory"
    class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${spring.jpa.database-platform}</prop>
            <prop key="hibernate.show_sql">${spring.jpa.show-sql}</prop>
            <prop key="hibernate.format_sql">${spring.jpa.format-sql}</prop>
            <prop key="hibernate.hbm2ddl.auto">${spring.jpa.hibernate.ddl-auto}</prop>
            <prop key="hibernate.generate_statistics">true</prop>               
        </props>
    </property>

否则这些休眠属性将不会仅通过使用 application.properties 来设置。但是数据源是通过读取 application.properties 文件自动创建的。谁能告诉我休眠属性缺少什么?我绝对希望保留 appContext.xml,因为将其更改为使用注释只需要大量重构。

这是我的 application.properties。

spring.datasource.type=org.apache.tomcat.jdbc.pool.DataSource
spring.datasource.url=xxxxxx
spring.datasource.username=xxxx
spring.datasource.password=xxxxx
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.tomcat.initial-size=15
spring.datasource.tomcat.max-wait=20000
spring.datasource.tomcat.max-active=50
spring.datasource.tomcat.max-idle=15
spring.datasource.tomcat.min-idle=8
spring.datasource.tomcat.default-auto-commit=true
spring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect
spring.jpa.hibernate.ddl-auto=none
spring.jpa.show-sql=true
spring.jpa.format-sql=false

【问题讨论】:

    标签: hibernate spring-boot


    【解决方案1】:

    只能通过application.properties设置。

        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${spring.jpa.database-platform}</prop>
                ...            
            </props>
        </property>
    

    spring.jpa.properties.hibernate.dialect=${spring.jpa.database-platform}
    ...
    

    祝你好运

    【讨论】:

    • “hibernate.show_sql”怎么样?我是否在 application.properties 中设置错误?
    • 没有。你没有出错。一个在 Jpa 层工作,一个在 Hibernate 层工作。 Jpa 正在包装 Hibernate。
    • application.properties 中的 hibernate.show_sql 在我的场景中不起作用。这就是我问这个问题的原因
    • hibernate.show_sql -> spring.jpa.properties.hibernate.show_sql
    猜你喜欢
    • 2017-01-24
    • 1970-01-01
    • 2019-06-15
    • 2018-08-03
    • 1970-01-01
    • 2013-02-08
    • 1970-01-01
    • 2012-01-06
    • 2021-02-22
    相关资源
    最近更新 更多