【问题标题】:Programatically setting the db url/user/password in Hibernate在 Hibernate 中以编程方式设置数据库 url/用户/密码
【发布时间】:2011-03-11 05:29:06
【问题描述】:

我需要将 Java Web 应用程序的所有设置集中在一个 .properties 文件中。我仍然可以使用 hibernate.cfg.xml 将映射添加到实体类,但我需要将数据库和自定义路径的所有设置保存在一个 .properties 文件中。

最初我将配置保存在 hibernate.cfg.xml 中,如下所示......

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="connection.url">my jdbc connection</property>
        <property name="connection.driver_class">oracle.jdbc.OracleDriver</property>
        <property name="connection.username">user</property>
        <property name="connection.password">password</property>
        <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
        <property name="hibernate.current_session_context_class">managed</property>
        <mapping class="myEntityClass"/>
    </session-factory>
</hibernate-configuration>

现在我想将“connection.url”、“connection.username”和“connection.password”移动到我自己的 .properties 文件中。创建我的休眠配置类的代码来自。

new AnnotationConfiguration().configure();

new AnnotationConfiguration()
                .setProperty("connection.url", databaseUrl)
                .setProperty("connection.username", databaseUser)
                .setProperty("connection.password", databasePassword)
                .configure();

这在概念上似乎很简单。不幸的是,当我尝试使用与以前的配置一起使用的 Hibernate Session 时,出现以下错误。

用户必须提供 JDBC 连接

有什么想法吗?在我看来,当 Hibernate 发现 hibernate.cfg.xml 文件中缺少这些属性时,它假定所有设置都将手动添加并完全忽略 xml。

【问题讨论】:

    标签: java hibernate jdbc


    【解决方案1】:
    spring.datasource.username=root
    spring.datasource.password=root
    spring.datasource.initialize=true
    spring.flyway.baseline-on-migrate=true
    flyway.baseline-on-migrate: true
    spring.flyway.baselineVersionAsString=2
    spring.quartz.job-store-type=jdbc
    spring.quartz.properties.org.quartz.scheduler.instanceId=AUTO
    spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
    spring.jpa.hibernate.ddl-auto = update
    spring.jpa.show-sql=true
    

    【讨论】:

    • 虽然此代码可能会回答问题,但提供有关此代码为何和/或如何回答问题的额外上下文可提高其长期价值。
    【解决方案2】:

    尝试设置以下属性

    properties.put("hibernate.connection.driver_class", "net.sourceforge.jtds.jdbc.Driver");
    properties.put("hibernate.connection.url", "jdbc:jtds:sqlserver://test/dbname;SSL=REQUEST");
    properties.put("hibernate.connection.username", "user");
    properties.put("hibernate.connection.password", "password");
    properties.put("hibernate.dialect", "org.hibernate.dialect.SQLServerDialect");
            
    

    当然这是针对 SQL Server 的,因此您需要将驱动程序更改为 'org.gjt.mm.mysql.Driver"' 并更改方言 'org.hibernate.dialect.MySQLInnoDBDialect'

    【讨论】:

      【解决方案3】:

      来自 Hibernate 参考文档:

      3.3. JDBC connections

      [...]

      以下是一个例子 hibernate.properties c3p0 的文件:

      hibernate.connection.driver_class = org.postgresql.Driver
      hibernate.connection.url = jdbc:postgresql://localhost/mydatabase
      hibernate.connection.username = myuser
      hibernate.connection.password = secret
      hibernate.c3p0.min_size=5
      hibernate.c3p0.max_size=20
      hibernate.c3p0.timeout=1800
      hibernate.c3p0.max_statements=50
      hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
      

      根据您的需要调整它并hibernate.properties 放在类路径的根目录中(并从hibernate.cfg.xml 中删除等效条目,因为XML 配置文件会覆盖属性)。所以其实不需要改下面这行:

      new AnnotationConfiguration().configure();
      

      除非您真的当然想要程序化配置。

      但从您的问题正文中,移动到 .properties 文件是另一回事,您可以依赖 Hibernate:将相关属性从 hibernate.cfg.xml 移动到 hibernate.properties。 p>

      【讨论】:

      • 为什么“connection.url”在hibernate.cfg.xml文件中起作用?它是否假设您的意思是 hibernate.connection.url?这很有趣。
      • @Benju:嗯,正如您在org.hibernate.cfg.Environment 中看到的那样,属性以hibernate. 为前缀。也许 Hibernate 开发人员正在使用 XML 配置做一些魔术,我没有检查源代码。无论如何,文档很清楚。
      • @Benju 我认为 XML 中指定的属性可以使用或不使用 hibernate. 前缀。请参阅org.hibernate.cfg.Configuration.addProperties(Element) 方法。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-08-04
      • 1970-01-01
      • 1970-01-01
      • 2022-01-16
      • 1970-01-01
      • 2011-08-29
      • 2015-03-17
      相关资源
      最近更新 更多