-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Help on Accessing 2 Different DBs
PostPosted: Wed Apr 14, 2010 5:12 pm 
Newbie

Joined: Wed Apr 14, 2010 5:04 pm
Posts: 3
Hello,

Hello!

I'm new to Hibernate, the thing is that, right now I have one database, but I'm being asked to extract a group of tables from that database, and create a new one so, I need to know a way to access to two databases using Hibernate, The way I'm asking for the sessionFactory is as follows:
Code:
public class HibernateUtil {
    private static final SessionFactory sessionFactory;

    static {
        try {
            // Create the SessionFactory from standard (hibernate.cfg.xml)
            // config file.
            sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            // Log the exception.
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}

**** This is the template class which Netbeans creates.

I have a XML configuration file (hibernate.cfg.xml) and the mappings as XMLs too, so, what do I have to do in order to be able to access both databases? What do I have to change? If you have useful examples/articles to perform this, it will be very useful,

Thanks!!!

Alberto Cole


Top
 Profile  
 
 Post subject: Re: Help on Accessing 2 Different DBs
PostPosted: Thu Apr 15, 2010 11:15 am 
Newbie

Joined: Wed Apr 14, 2010 5:04 pm
Posts: 3
Anyone?


Top
 Profile  
 
 Post subject: Re: Help on Accessing 2 Different DBs
PostPosted: Thu Apr 15, 2010 11:28 am 
Regular
Regular

Joined: Tue Feb 24, 2004 11:42 am
Posts: 56
Just create 2 session factory in 2 different .cfg files and load them up manually.Hope this helps
Please do not forget to rate.


Top
 Profile  
 
 Post subject: Re: Help on Accessing 2 Different DBs
PostPosted: Thu Apr 15, 2010 12:05 pm 
Newbie

Joined: Wed Apr 14, 2010 5:04 pm
Posts: 3
gopalsaha wrote:
Just create 2 session factory in 2 different .cfg files and load them up manually.Hope this helps
Please do not forget to rate.


Ok, the easy part is creating the new configuration file, but, I'm not sure how to create a sessionFactory for each configuration file

Code:
sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();


What should I change in this line to say in a explicit way which config file is going to be related to that sessionFactory?

Thanks!


Top
 Profile  
 
 Post subject: Re: Help on Accessing 2 Different DBs
PostPosted: Fri Apr 16, 2010 8:58 am 
Regular
Regular

Joined: Tue Feb 24, 2004 11:42 am
Posts: 56
Configuration c1 =new AnnotationConfiguration().configure("db1.cfg.xml")
Configuration c2= new AnnotationConfiguration().configure("db2.cfg.xml")

sessionfactory1 = c1.buildSessionFactory();
sessionfactory2 = c2.buildSessionFactory();
Hope this helps
Please dont forget to rate


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.