我们可以在没有hibernate.cfg.xml的情况下配置Hibernate吗


问题内容

以下是hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.DerbyDialect</property>
<property name="hibernate.connection.driver_class">org.apache.derby.jdbc.ClientDriver</property>
<property name="hibernate.connection.url">jdbc:derby://localhost:1527/XE</property>
<property name="hibernate.connection.username">username</property>
<property name="hibernate.connection.password">password</property>
</session-factory>
</hibernate-configuration>

我想知道是否总是有必要hibernate.cfg.xml在每个Hibernate应用程序中使用它,或者是否有其他替代方法来配置Hibernate。


问题答案:

例如,您可以将hibernate.cfg.xml的属性指定为spring bean.xml中的属性注入

<property name="hibernateProperties">  
    <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
    </props>
    </property>

因此,您可以类似的方式将spring中的所有属性指定为sessionFacory的依赖项