【问题标题】:Have 2 hibernate.properties files有 2 个 hibernate.properties 文件
【发布时间】:2017-08-01 16:06:36
【问题描述】:

我有一个带有 Hibernate 的 Spring Boot 项目。我想使用 2 个单独的 hibernate.properties 文件:一个用于正常使用应用程序,另一个仅用于测试。

那么,我该怎么做呢?或者也许这是不可能的?我不想在此文件中注释/取消注释行以进行测试。

感谢您的每一个回答和帮助。

#hibernate.connection.driver_class = org.postgresql.Driver
#hibernate.connection.url = jdbc:postgresql://localhost:5432/dummydb
#hibernate.connection.username = postgres
#hibernate.connection.password = postgres
#hibernate.dialect = org.hibernate.dialect.PostgreSQL91Dialect

hibernate.connection.driver_class = org.hsqldb.jdbcDriver
hibernate.connection.url = jdbc:hsqldb:mem:dummydb
hibernate.connection.username = user
hibernate.connection.password = password
hibernate.hbm2ddl.auto=create
hibernate.dialect = org.hibernate.dialect.HSQLDialect

hibernate.show_sql=true

【问题讨论】:

    标签: database hibernate testing junit spring-data-jpa


    【解决方案1】:

    您可以使用Spring profiles 来分离生产和测试环境。

    在您的情况下,您需要创建 2 个属性文件:application.propertyapplication-test.property。首先必须包含您的生产环境,其次是测试环境。

    然后你需要在执行单元测试时 chouse test 配置文件。只需将@ActiveProfiles(profiles = "test") 添加到您的测试类即可。

    【讨论】:

    • 为什么要为 Spring Boot 应用程序使用休眠属性文件。您可以直接在 application.properties 文件中配置休眠属性。或者,如果您使用 yaml,则在 application.yml 文件中。 Configure JPA Properties
    猜你喜欢
    • 1970-01-01
    • 2016-06-29
    • 2014-07-17
    • 2012-01-19
    • 2011-09-22
    • 1970-01-01
    • 2013-07-02
    • 1970-01-01
    • 2019-03-06
    相关资源
    最近更新 更多