多个LocalSessionFactoryBean不起作用

时间:2013-12-16 21:40:59

标签: java spring hibernate sessionfactory

我有多个数据源,动态会话映射在我使用“org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean”时工作正常现在我已经将我的应用程序升级到hibernate 4并开始使用“org.springframework”。 orm.hibernate4.LocalSessionFactoryBean”。出于某种原因,hibernate无法映射我的第二个数据源中的表。

这是我的配置。

<bean id="DataSource1" class="org.apache.commons.dbcp.BasicDataSource"
    autowire="byName" destroy-method="close">
    <property name="driverClassName" value="$api{d1.jdbc.driver}" />
    <property name="url" value="$api{d1.jdbc.url}" />
    <property name="username" value="$api{d1.jdbc.username}" />
    <property name="password" value="$api{d1.jdbc.password}" />
    <property name="maxActive" value="$api{dbcp.maxActive}" />
    <property name="maxWait" value="$api{dbcp.maxWait}" />
    <property name="minIdle" value="$api{dbcp.minIdle}" />
    <property name="maxIdle" value="$api{dbcp.maxIdle}" />
    <property name="validationQuery" value="$api{dbcp.validationQuery}" />
    <property name="testOnBorrow" value="true" />
</bean>
<bean id="d1SessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
    autowire="byName">
    <property name="dataSource" ref="DataSource1" />
    <property name="annotatedClasses">
        <list>
            <value>com.class1</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">false</prop>
            <prop key="hibernate.hbm2ddl.auto">validate</prop>
            <prop key="hibernate.current_session_context_class">thread</prop>
            <prop key="hibernate.transaction.flush_before_completion">true</prop>
            <prop key="hibernate.transaction.auto_close_session">true</prop>

        </props>
    </property>
</bean>
<bean id="d1Dao" class="com.DaoImpl"
    autowire="byName">
    <property name="sessionFactory" ref="d1SessionFactory"></property>
</bean>

第二个会话定义是

<bean id="DataSource2" class="org.apache.commons.dbcp.BasicDataSource"
    autowire="byName" destroy-method="close">
    <property name="driverClassName" value="$api{d1.jdbc.driver}" />
    <property name="url" value="$api{d1.jdbc.url}" />
    <property name="username" value="$api{d2.jdbc.username}" />
    <property name="password" value="$api{d2.jdbc.password}" />
    <property name="maxActive" value="$api{dbcp.maxActive}" />
    <property name="maxWait" value="$api{dbcp.maxWait}" />
    <property name="minIdle" value="$api{dbcp.minIdle}" />
    <property name="maxIdle" value="$api{dbcp.maxIdle}" />
    <property name="validationQuery" value="$api{dbcp.validationQuery}" />
    <property name="testOnBorrow" value="true" />
</bean>
<bean id="d2SessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
    autowire="byName">
    <property name="dataSource" ref="DataSource2" />
    <property name="annotatedClasses">
        <list>
            <value>com.class2</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">false</prop>
            <prop key="hibernate.hbm2ddl.auto">validate</prop>
            <prop key="hibernate.current_session_context_class">thread</prop>
            <prop key="hibernate.transaction.flush_before_completion">true</prop>
            <prop key="hibernate.transaction.auto_close_session">true</prop>

        </props>
    </property>
</bean>
<bean id="d2Dao" class="com.DaoImpl"
    autowire="byName">
    <property name="sessionFactory" ref="d2SessionFactory"></property>
</bean>

在我执行的运行时

Session session = this.sessionFactory.openSession() ; 

即使dao是d2Dao,会话也始终对应于d1SessionFactory。 不知道我在这里做错了什么。 当我使用“AnnotationSessionFactoryBean”

时,相同的配置工作正常

帮助表示赞赏。

我得到的错误如下

org.hibernate.hql.internal.ast.QuerySyntaxException: Class2 is not mapped [FROM Class2 WHERE user_id = :value0 AND active = :value1] at org.hibernate.hql.internal.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:180) at 
org.hibernate.hql.internal.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:110) at org.hibernate.hql.internal.ast.tree.FromClause.addFromElement(FromClause.java:93) at 
org.hibernate.hql.internal.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:326) at 
org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3252) at 
org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3141) at 
org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:694) at 
org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:550) at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:287) at 
org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:235) at 
org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:248) at 
org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:183) at 
org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136) at 
org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:101) at 
org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:80) at 
org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:119) at 
org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:215) at 
org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:193) at 
org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1649) 

0 个答案:

没有答案