【发布时间】: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。
【问题讨论】: