Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7711073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:17:04+00:00 2026-06-01T01:17:04+00:00

There have been multiple discussions on this topic in the site but I am

  • 0

There have been multiple discussions on this topic in the site but I am using annotations all across to create sessionFactory.

private SessionFactory sessionFactory;

@Autowired
public void setSessionFactory(SessionFactory sessionFactory) {
    this.sessionFactory = sessionFactory;

This is how hibernate.cfg.xml looks like

<hibernate-configuration>
<session-factory>
    <property name="hibernate.bytecode.use_reflection_optimizer">false</property>
    <property name="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.password">xxxxx</property>
    <property name="hibernate.connection.url">xxxxx</property>
    <property name="hibernate.connection.username">XXXX</property>
    <property name="hibernate.connection.characterEncoding">utf-8</property>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
    <property name="hibernate.format_sql">true</property>
    <property name="hibernate.show_sql">true</property>
    <property name="hibernate.use_sql_comments">true</property>
    <mapping class="XXXX" />
</session-factory>
</hibernate-configuration>

This is applicationContext.xml

<!-- add tomcat datasource instance to springs context -->
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="java:comp/env/jdbc/XXXXDS" />
    <property name="resourceRef" value="true" />
</bean>

How do I achieve multiple DB integration in this case. One solution that I came across was to create multiple hibernate.cfg.xml and create another session factory for it.

But I have two doubts, 1) How do I indicate in autowiring, which sessionFactory to pick, 2) I will have to duplicate lot of info across hibernate.xml, for example beans. Is there any cleaner approach for this issue?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T01:17:05+00:00Added an answer on June 1, 2026 at 1:17 am

    First question: @Autowired with @Qualifier(“name”) will help you to pick which sessionFatory.

    @Autowired
    @Qualifier("sessionFactoryName")
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    ...
    }
    

    Second question: you can set hibernate properties: schema and catalog to specify database when working on multiple databases. I have the experience with MS SQL Server, so in entity I added annotation @Table(“[database].[schema].[tablename]”) and it mapping to the specific table in multiple databases.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There have been many debates about this topic already here, but none of them
This may have been asked multiple times, but is there a good HTML-to-PDF creator
There have been a couple of questions very close to this topic, but none
There have been a couple of questions that sort of dealt with this but
There have been a few questions like this around the place but none have
I have been reading here and there about multiple JQuery selectors but I couldn't
I have been trying to force images to download using PHP Headers but there
I've searched SO and this question seems to have been asked multiple times, but
There are multiple values I have been storing in ASP.NET configSections sections for each
There have been a few timely posts about IP security and the like, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.