Java 多休眠配置

Java 多休眠配置,java,hibernate,Java,Hibernate,我目前正在构建一个库来模块化我的一些代码,而Hibernate遇到了一个问题 在我的主应用程序中,我有一个hibernate配置来获取它需要运行的信息,但是我的库中也需要hibernate,因为我想要的一些对象可以在其他应用程序中使用 当我启动tomcat服务器时,在两种hibernates设置下,我都会收到一些错误,说明bean无法解析,还有一个错误说我的查询中缺少位置参数。但是,当我仅使用应用程序Hibernate config启动Tomcat时,它启动良好 下面是配置的样子 从图书馆: &

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

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

当我启动tomcat服务器时,在两种hibernates设置下,我都会收到一些错误,说明bean无法解析,还有一个错误说我的查询中缺少位置参数。但是,当我仅使用应用程序Hibernate config启动Tomcat时,它启动良好

下面是配置的样子

从图书馆:

<?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>

从应用程序中:

<?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>
SessionFactory sf = new Configuration()
        .configure("Fromtheapp.cfg.xml")
        .buildSessionFactory();

org.hibernate.cache.EhCacheProvider
真的
假的

我对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();

编辑:未打印会话工厂,因为它包含大于或小于个字符。

如果要正确执行,请使用hibernate shard。否则,只需传递要使用的hibernate.cfg.xml的路径(在文件系统或类路径中)

从图书馆

SessionFactory sf = new Configuration()
    .configure("Fromthelibrary.cfg.xml")
    .buildSessionFactory();
从应用程序中:

<?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>
SessionFactory sf = new Configuration()
        .configure("Fromtheapp.cfg.xml")
        .buildSessionFactory();

这似乎是我要找的,但让我问一下。让我的库有它自己的会话工厂可以使用是更好的,还是应该为整个过程提供一个会话工厂?在我的情况下,多个会话工厂似乎更合适,但我不知道如果我这样做,是否会遇到麻烦。老实说,我对Hibernate没有太多经验。然而,从您描述的内容来看,我认为多个SessionFactory对象似乎适合您的用例,因为它有助于模块化您的代码。但请不要相信我的话,因为我甚至不知道是否会对性能造成影响,例如,是否会出现任何其他问题。感谢您的帮助,我非常肯定倍数是一个不错的选择。我要带着它跑,希望它不会回来咬我!再次感谢!