Announcement Announcement Module
Collapse
No announcement yet.
Hibernate using multiple databases Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Hibernate using multiple databases

    Hello everybody

    Someone know how to add a another datasource in hibernate configuration and how to configure Spring to that datasource its autoinject in my respective DAO?


    This is my code with one datasource, that run perfectly, but i don't know how add another datasource.


    HIBERNATE CONFIGURACION

    Code:
      <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName">
                <value>com.mysql.jdbc.Driver</value>
            </property>
            <property name="url" value="jdbc:mysql://localhost/personal"/>
            <property name="username" value="root"/>
            <property name="password" value="mysql"/>
        </bean>
    
       
    
        
        <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
            <property name="dataSource">
                <ref local="dataSource"/>
            </property>
            <property name="packagesToScan">
                <list>
                    <value>com.app.personal.model</value>
                </list>
            </property>
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                    <prop key="hibernate.show_sql">false</prop>
                </props>
            </property>
        </bean>
    
        <tx:annotation-driven/>
        <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            <property name="sessionFactory">
                <ref local="sessionFactory"/>
            </property>
        </bean>


    DAO EXAMPLE


    Code:
    @Repository
    public class ModuloDAOHibernate extends HibernateTemplate implements ModuloDAO {
    
        @Autowired
        public ModuloDAOHibernate(SessionFactory sessionFactory) {
            super(sessionFactory); 
        }
    
        public List<Modulo> getAllGrupoModuloDAO() {
            Criteria criteriaList = this.getSession().createCriteria(Modulo.class);
            criteriaList.addOrder(Order.asc("orden"));
            return criteriaList.list();
        }



    Thanks

  • #2
    Hello Jose

    Someone know how to add a another datasource in hibernate configuration and how to configure Spring to that datasource its autoinject in my respective DAO?
    Do you want for your unique Hibernate SessionFactory work with two datasources? I think is not possible

    What is your specific requirement to want do this?

    Comment


    • #3
      Well. I've a aplication that should access to two databases. At this time my configuration only can access to one database ( like example: personal) but I've access to another database and I want to access too to that.

      Comment


      • #4
        Hello

        Did you reseach about this in the forum? I recall this was covered some times


        Perhaps you could create two different SessionFactory's with dependency to two different dataSource (for each DB configuration) and inject carefully in your DAO classes

        I assume each DB is totally independt of each other

        Try google too, here a result Load Two DataSources

        Comment


        • #5
          Well. Finally I resolved my problem with this way:

          First: add another dataSource and sessionFactory.

          Second: add in the end of conf of hibernate a bean to every DAO that use another sessionFactory (implicit another dataSource). Like this:


          Code:
          <bean id="courseDAO" class="com.app.CourseDAOHibernate">
                  <property name="sessionFactory" ref="sessionFactory2"/>
              </bean>

          Every DAO that use a "sessionFactory" is inyected automatically, but in case of others DAO that use a others datasource you should inyect explicitely like the example.

          In the end I don't know if that is a successful solution but that is working for me.

          Comment


          • #6
            OK. When one Light turn on, another Lights turn on too. I 've looking for another solutions, and find this:

            Use the @Qualifier Annotation on the method that inject the sessionFactory:

            Code:
             @Autowired
                public ProgramaDAOHibernate(@Qualifier("sessionFactory2") SessionFactory sessionFactory) {
                    super(sessionFactory);
                }


            WOW. Thank to everyone that helpme.

            Comment

            Working...
            X