Note that there are some explanatory texts on larger screens.

plurals
  1. POMultiple LocalSessionFactoryBean does not work
    primarykey
    data
    text
    <p>I have multiple data sources and the dynamic session mapping used to work fine when I used "org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" Now I have upgraded my application to hibernate 4 and started using "org.springframework.orm.hibernate4.LocalSessionFactoryBean". For some reason hibernate is not able to map the tables from my second data source. </p> <p>Here are my configs. </p> <pre><code>&lt;bean id="DataSource1" class="org.apache.commons.dbcp.BasicDataSource" autowire="byName" destroy-method="close"&gt; &lt;property name="driverClassName" value="$api{d1.jdbc.driver}" /&gt; &lt;property name="url" value="$api{d1.jdbc.url}" /&gt; &lt;property name="username" value="$api{d1.jdbc.username}" /&gt; &lt;property name="password" value="$api{d1.jdbc.password}" /&gt; &lt;property name="maxActive" value="$api{dbcp.maxActive}" /&gt; &lt;property name="maxWait" value="$api{dbcp.maxWait}" /&gt; &lt;property name="minIdle" value="$api{dbcp.minIdle}" /&gt; &lt;property name="maxIdle" value="$api{dbcp.maxIdle}" /&gt; &lt;property name="validationQuery" value="$api{dbcp.validationQuery}" /&gt; &lt;property name="testOnBorrow" value="true" /&gt; &lt;/bean&gt; &lt;bean id="d1SessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" autowire="byName"&gt; &lt;property name="dataSource" ref="DataSource1" /&gt; &lt;property name="annotatedClasses"&gt; &lt;list&gt; &lt;value&gt;com.class1&lt;/value&gt; &lt;/list&gt; &lt;/property&gt; &lt;property name="hibernateProperties"&gt; &lt;props&gt; &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.MySQLDialect&lt;/prop&gt; &lt;prop key="hibernate.show_sql"&gt;false&lt;/prop&gt; &lt;prop key="hibernate.hbm2ddl.auto"&gt;validate&lt;/prop&gt; &lt;prop key="hibernate.current_session_context_class"&gt;thread&lt;/prop&gt; &lt;prop key="hibernate.transaction.flush_before_completion"&gt;true&lt;/prop&gt; &lt;prop key="hibernate.transaction.auto_close_session"&gt;true&lt;/prop&gt; &lt;/props&gt; &lt;/property&gt; &lt;/bean&gt; &lt;bean id="d1Dao" class="com.DaoImpl" autowire="byName"&gt; &lt;property name="sessionFactory" ref="d1SessionFactory"&gt;&lt;/property&gt; &lt;/bean&gt; </code></pre> <p>And the second session definition is </p> <pre><code>&lt;bean id="DataSource2" class="org.apache.commons.dbcp.BasicDataSource" autowire="byName" destroy-method="close"&gt; &lt;property name="driverClassName" value="$api{d1.jdbc.driver}" /&gt; &lt;property name="url" value="$api{d1.jdbc.url}" /&gt; &lt;property name="username" value="$api{d2.jdbc.username}" /&gt; &lt;property name="password" value="$api{d2.jdbc.password}" /&gt; &lt;property name="maxActive" value="$api{dbcp.maxActive}" /&gt; &lt;property name="maxWait" value="$api{dbcp.maxWait}" /&gt; &lt;property name="minIdle" value="$api{dbcp.minIdle}" /&gt; &lt;property name="maxIdle" value="$api{dbcp.maxIdle}" /&gt; &lt;property name="validationQuery" value="$api{dbcp.validationQuery}" /&gt; &lt;property name="testOnBorrow" value="true" /&gt; &lt;/bean&gt; &lt;bean id="d2SessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" autowire="byName"&gt; &lt;property name="dataSource" ref="DataSource2" /&gt; &lt;property name="annotatedClasses"&gt; &lt;list&gt; &lt;value&gt;com.class2&lt;/value&gt; &lt;/list&gt; &lt;/property&gt; &lt;property name="hibernateProperties"&gt; &lt;props&gt; &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.MySQLDialect&lt;/prop&gt; &lt;prop key="hibernate.show_sql"&gt;false&lt;/prop&gt; &lt;prop key="hibernate.hbm2ddl.auto"&gt;validate&lt;/prop&gt; &lt;prop key="hibernate.current_session_context_class"&gt;thread&lt;/prop&gt; &lt;prop key="hibernate.transaction.flush_before_completion"&gt;true&lt;/prop&gt; &lt;prop key="hibernate.transaction.auto_close_session"&gt;true&lt;/prop&gt; &lt;/props&gt; &lt;/property&gt; &lt;/bean&gt; &lt;bean id="d2Dao" class="com.DaoImpl" autowire="byName"&gt; &lt;property name="sessionFactory" ref="d2SessionFactory"&gt;&lt;/property&gt; &lt;/bean&gt; </code></pre> <p>At run time when I do </p> <pre><code>Session session = this.sessionFactory.openSession() ; </code></pre> <p>The session always corresponds to d1SessionFactory even when dao is d2Dao. Not sure what am I doing wrong here. The same config worked fine when I was using "AnnotationSessionFactoryBean"</p> <p>Help is appreciated. </p> <p>The error I get is as follows </p> <pre><code>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.&lt;init&gt;(HQLQueryPlan.java:101) at org.hibernate.engine.query.spi.HQLQueryPlan.&lt;init&gt;(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) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload