Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 发现了两个会话工厂,而预期为一个_Java_Spring_Hibernate - Fatal编程技术网

Java 发现了两个会话工厂,而预期为一个

Java 发现了两个会话工厂,而预期为一个,java,spring,hibernate,Java,Spring,Hibernate,我已提及在会议上提出的问题。我的问题类似,但我面临的问题不同。我创建了两个xml文件,每个文件都有单独的数据源和会话工厂。 在我的web.xml中,我有 <context-param> <param-name>contextConfigLocation</param-name> <param-value>*The xml files* </param-value> DaoHibernateEagerImpl类是 @Imp

我已提及在会议上提出的问题。我的问题类似,但我面临的问题不同。我创建了两个xml文件,每个文件都有单独的数据源和会话工厂。 在我的web.xml中,我有

 <context-param>
 <param-name>contextConfigLocation</param-name>
     <param-value>*The xml files* </param-value>
DaoHibernateEagerImpl类是

@Implementation
@Repository("daoEager")
public class DaoHibernateEagerImpl extends DaoHibernateImpl
{
// ========================= CONSTANTS =================================

/**
 * A logger that helps identify this class' printouts.
 */
private static final Logger log = getLogger(DaoHibernateEagerImpl.class);

// ========================= CONSTRUCTORS ==============================

/**
 * Required for a Spring DAO bean.
 *
 * @param sessionFactory
 *            Hibernate session factory
 */
@Autowired
public DaoHibernateEagerImpl(final SessionFactory sessionFactory)
{
    super(sessionFactory);
}

// ========================= DEPENDENCIES ==============================

// ========================= IMPLEMENTATION: Dao =======================



// ========================= IMPLEMENTATION: SearchEngine ==============


}
其中一个xml文件是

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"                  
    xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"     
    xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

    <context:annotation-config />

<!--
    Data source: reads a properties file and injects them into a DBCP DS
    Second datasource for Resource Profiles
-->
<bean id="profiledataSource"
    class=".....ConfigurableBasicDataSource">
    <constructor-arg index="0">
        <bean class="org.apache.commons.dbcp.BasicDataSource" />
    </constructor-arg>
    <property name="properties">
        <bean

    class="org.springframework.beans.factory.config.PropertiesFactoryBean">
            <property name="locations">
                <list>
                    <value>WEB-INF/datasource-local.properties
                    </value>
                </list>
            </property>
        </bean>
    </property>

    <!-- FUR-946: idle connections break. Adding connection testing. -->
    <property name="testOnBorrow" value="true" />
    <property name="testWhileIdle" value="true" />
</bean>




    <!-- Session factory -->
<!-- Session Factory for the second datasource-->
<bean id="profilesessionFactory"


   class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="profiledataSource" />

    <!--
        Hibernate configuration properties (read from a properties file)
    -->
    <property name="hibernateProperties">
        <bean

    class="org.springframework.beans.factory.config.PropertiesFactoryBean">
            <property name="locations">
                <list>
                    <value>WEB-INF/hibernate.properties
                    </value>
                    <value>WEB-INF/datasource-local.properties
                    </value>

                </list>
            </property>
        </bean>
    </property>

    <!-- Using improved naming strategy -->
    <property name="namingStrategy">
        <bean class="org.hibernate.cfg.DefaultNamingStrategy" />
    </property>




    <!-- Mapping annotated classes using search patterns -->
    <property name="annotatedClasses">
        <list>
            <value><![CDATA[....profiledb.domain.Profiles]]></value>
        </list>
    </property>
</bean>

 <!--   Hibernate data access template -->
 <bean id="profilehibernateTemplate"      
  class="org.springframework.orm.hibernate3.HibernateTemplate">

  <property name="sessionFactory" ref="profilesessionFactory" />
</bean>


<tx:annotation-driven />

<!-- a PlatformTransactionManager is still required -->
<bean id="profiletransactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="profilesessionFactory" />
</bean>


<bean id="profilesdbDao" class="....profiledb.service.ProfilesDaoImpl" >
    <property name="sessionFactory" ref="profilesessionFactory"></property>
另一个xml文件类似,但具有不同的数据源,并且会话工厂是

<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
 .....

错误信息非常清楚:

应为单个匹配bean,但找到2:[sessionFactory,profilesessionFactory]

这是可以理解的:

<bean id="profilesessionFactory"
<!-- ... -->
<bean id="sessionFactory"
如果您不能修改DaoHibernateEagerImpl类,那么您可以始终退回到基于XML的配置。首先禁用DaoHibernateEagerImpl的类路径扫描,以便不拾取@Autowired。然后简单地写下:

<bean class="DaoHibernateEagerImpl">
   <constructor-arg ref="profilesessionFactory"/>
   <!-- ... -->
</bean>

当自动连线时,这将首选sessionFactory,并且有两种可能而不是引发异常。

错误消息非常清楚:

应为单个匹配bean,但找到2:[sessionFactory,profilesessionFactory]

这是可以理解的:

<bean id="profilesessionFactory"
<!-- ... -->
<bean id="sessionFactory"
如果您不能修改DaoHibernateEagerImpl类,那么您可以始终退回到基于XML的配置。首先禁用DaoHibernateEagerImpl的类路径扫描,以便不拾取@Autowired。然后简单地写下:

<bean class="DaoHibernateEagerImpl">
   <constructor-arg ref="profilesessionFactory"/>
   <!-- ... -->
</bean>

当自动连接时,这将更喜欢sessionFactory,有两种可能性而不是引发异常。

这是因为……如果您在xml配置中没有提到@Primaryannotation/primary=True,Hiibernate不知道选择哪个sessionFactory。在这种情况下,它将给出所提到的错误。只需尝试使用@Primary annotation它就可以工作了…

这是因为…如果您在xml配置中没有提到@Primary annotation/Primary=true,Hiibernate不知道选择哪个sessionfactory。在这种情况下,它会给您提到的错误。只需使用@Primary注释即可…

请提供bean定义xml以及此异常中列出的类DaoHibernateEagerImpl,那么只有有人可以帮助你。我已经按照你的建议添加了更多的细节。请提供bean定义xml以及这个异常中列出的类DaoHibernateEagerImpl,那么只有有人可以帮助你。我已经按照你的建议添加了更多的细节。谢谢。但是有没有办法不用修改DaoHibernateEagerImpl就可以做到这一点?这是因为我将该类用作maven中的依赖项,其余部分是特定于项目的,但我无法修改该特定类。谢谢,sessionFactory应该是默认的会话工厂,profilesessionFactory必须是特定daoimpl的会话工厂。正如您所提到的,我在xml文件中添加了这一行。现在我得到了错误:主错误[org.hibernate.util.XMLHelper]-分析XML时出错:core-profile.xml2文档无效:找不到语法。主要错误[org.hibernate.util.XMLHelper]-解析XML:core-profile.xml2文档根元素bean时出错,必须与DOCTYPE root null匹配。谢谢。但是有没有办法在不修改DaoHibernateEagerImpl的情况下完成此操作?这是因为我将该类用作maven中的依赖项,其余部分是特定于项目的,但我无法修改该特定类。谢谢,sessionFactory应该是默认的会话工厂,profilesessionFactory必须是特定daoimpl的会话工厂。正如您所提到的,我在xml文件中添加了这一行。现在我得到了错误:主错误[org.hibernate.util.XMLHelper]-分析XML时出错:core-profile.xml2文档无效:找不到语法。主要错误[org.hibernate.util.XMLHelper]-解析XML:core-profile.xml2文档根元素bean时出错,必须与DOCTYPE root null匹配。
<bean class="DaoHibernateEagerImpl">
   <constructor-arg ref="profilesessionFactory"/>
   <!-- ... -->
</bean>
<bean id="sessionFactory" primary="true">
   <!-- ... -->