我们可以在没有hibernate.cfg.xml的情况下配置Hibernate吗? [英] Can we configure Hibernate Without hibernate.cfg.xml

查看:101
本文介绍了我们可以在没有hibernate.cfg.xml的情况下配置Hibernate吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是 hibernate.cfg.xml

  <?xml version =1.0encoding =UTF-8?> 
<!DOCTYPE hibernate-configuration PUBLIC - // Hibernate / Hibernate Configuration DTD 3.0 // ENhttp://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>用户名< / property>
< property name =hibernate.connection.password>密码< / property>
< / session-factory>
< / hibernate-configuration>

我是否总是需要使用 hibernate.cfg.xml code>在每个Hibernate应用程序中,或者有任何其他的配置Hibernate的方法。 hibernate.cfg.xml作为spring bean.xml中的属性注入
例如

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

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


The below is the 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>

I wonder if it's always necessary to use hibernate.cfg.xml in every Hibernate Application or there is any alternative way to configure Hibernate.

解决方案

You can Specify the properties of hibernate.cfg.xml as property injection in spring bean.xml for example

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

So in similar way you can specify all properties in spring as a dependency for sessionFacory

这篇关于我们可以在没有hibernate.cfg.xml的情况下配置Hibernate吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆