多个Hibernate配置 [英] Multiple Hibernate configs

查看:117
本文介绍了多个Hibernate配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在建立一个库来模块化我的一些代码,并且我遇到了Hibernate的一个问题。

在我的主应用程序中,我有一个hibernate配置来获取它需要运行的信息,但是我也需要在我的库中使用hibernate,因为我想要的一些对象可以用在其他应用程序中。



当我启动我的tomcat服务器时,同时安装了两个hibernates,我收到错误,指出bean无法解析,并且有一个说我的位置参数在我的查询中缺失。但是,当我启动Tomcat时只使用Hibernate配置应用程序就可以开始正常工作。



以下是configs的样子...





 <?xml version ='1.0'encoding ='utf-8' ?> 
<!DOCTYPE hibernate-configuration PUBLIC
- // Hibernate / Hibernate配置DTD 3.0 // EN

http://hibernate.sourceforge.net/hibernate构型-3.0.dtd>

< hibernate-configuration>


< session-factory>
< mapping resource =blah.hbm.xml/>
< mapping resource =blargh.hbm.xml/>
< mapping resource =stuff.hbm.xml/>
< mapping resource =junk.hbm.xml/>
< mapping resource =this.hbm.xml/>
< / session-factory>

< / hibernate-configuration>

并从应用程序中:

 <?xml version ='1.0'encoding ='utf-8'?> 
<!DOCTYPE hibernate-configuration PUBLIC
- // Hibernate / Hibernate配置DTD 3.0 // EN

http://hibernate.sourceforge.net/hibernate构型-3.0.dtd>

< hibernate-configuration>


< session-factory>

< property name =hibernate.cache.provider_class> org.hibernate.cache.EhCacheProvider< / property>

<! - 启用查询缓存 - >
< property name =hibernate.cache.use_query_cache> true< / property>

<! - 将所有执行的SQL回复到stdout - >
< property name =show_sql> false< / property>

<! - 映射文件 - >

< mapping resource =appStuff/>
< mapping resource =appBlah/>
< mapping resource =appBlargh/>
< mapping resource =appJunk/>
< mapping resource =appThis/>

< / session-factory>

< / hibernate-configuration>

对于Hibernate来说,我还很新,这是一种奇怪的配置。

解决方案

您可以以编程方式加载hibernate配置文件。

  SessionFactory sf = new Configuration().configure(somename.cfg.xml)。buildSessionFactory(); 

这将允许您创建两个SessionFactory对象。不过,我假设你想为你的应用和你的模块使用相同的SessionFactory。

您可以将两个hibernate XML文件加载到单个DOM对象中(将您的模块的会话工厂标签子代与您的应用程序的子代码结合),然后使用以下代码:

  import org.hibernate.cfg.Configuration; 
// ...
SessionFactory sf = new Configuration()。configure(yourDOMObject).buildSessionFactory();

编辑:会话工厂未打印,因为它具有大于和小于字符。

I'm currently working on building a library to modularize some of my code and I'm running into a problem with Hibernate.

In my main application I have a hibernate config to get information it needs to run but then I also have a need for hibernate in my library since some of the objects I want could be used in other applications.

When I start up my tomcat server, with both hibernates setup, I get errors stating that beans cannot be resolved and one that says my positional parameters are missing in my query. However, when I start up Tomcat with only the application Hibernate config it starts fine.

Here's what the configs look like...

From the library:

<?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>   
    <mapping resource="blah.hbm.xml"/>
    <mapping resource="blargh.hbm.xml"/>
    <mapping resource="stuff.hbm.xml"/>
    <mapping resource="junk.hbm.xml"/>
    <mapping resource="this.hbm.xml"/>
</session-factory>

</hibernate-configuration>

And from the application:

<?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.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>

    <!-- Enable the query cache  -->
    <property name="hibernate.cache.use_query_cache">true</property>

    <!-- Echo all executed SQL to stdout -->
    <property name="show_sql">false</property>

    <!-- mapping files -->

    <mapping resource="appStuff"/>
    <mapping resource="appBlah"/>
    <mapping resource="appBlargh"/>
    <mapping resource="appJunk"/>
    <mapping resource="appThis"/>    

</session-factory>

</hibernate-configuration>

I'm still pretty new to Hibernate and this is sort of a weird configuration.

解决方案

You can load hibernate configuration files programatically.

SessionFactory sf = new Configuration().configure("somename.cfg.xml").buildSessionFactory();

That would allow you to create two SessionFactory objects. However, I assume that you want to use the same SessionFactory for your app and your module.

You could load both hibernate XML files into a single DOM object (combine your module's "session-factory" tag children with your application's ones) and then use the following code:

import org.hibernate.cfg.Configuration;
// ...
SessionFactory sf = new Configuration().configure(yourDOMObject).buildSessionFactory();

Edit: session-factory wasn't printed because it had greater-than and less-than characters.

这篇关于多个Hibernate配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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