java 多个休眠配置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3449475/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Multiple Hibernate configs
提问by Shaded
I'm currently working on building a library to modularize some of my code and I'm running into a problem with Hibernate.
我目前正在构建一个库来模块化我的一些代码,但我遇到了 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.
当我启动我的 tomcat 服务器时,使用两个休眠设置,我收到错误,指出无法解析 bean,还有一个说我的位置参数在我的查询中丢失。但是,当我仅使用应用程序 Hibernate 配置启动 Tomcat 时,它启动正常。
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.
我对 Hibernate 还是很陌生,这是一种奇怪的配置。
回答by Tiago Alves
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.
这将允许您创建两个 SessionFactory 对象。但是,我假设您希望对您的应用程序和模块使用相同的 SessionFactory。
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:
您可以将两个休眠 XML 文件加载到单个 DOM 对象中(将模块的“session-factory”标签子项与应用程序的子项结合起来),然后使用以下代码:
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.
编辑: session-factory 没有打印,因为它有大于和小于字符。
回答by Sammyrulez
if you want to do it propely use hibernate shard 1. Othewise you can simply pass the path (on file system or in classpath ) of the hibernate.cfg.xml you want to use
如果你想这样做,请使用 hibernate shard 1。否则,您可以简单地传递您要使用的 hibernate.cfg.xml 的路径(在文件系统上或在 classpath 中)
From the library
从图书馆
SessionFactory sf = new Configuration()
.configure("Fromthelibrary.cfg.xml")
.buildSessionFactory();
And from the application:
并从应用程序:
SessionFactory sf = new Configuration()
.configure("Fromtheapp.cfg.xml")
.buildSessionFactory();

