- This topic has 3 replies, 3 voices, and was last updated 18 years, 3 months ago by MikeKing.
-
AuthorPosts
-
MikeKingMemberI have created an application that needs to read databaseA and both read and write databaseB (i.e. 2 databases).
Apparently I need to configure 2 different datasources and use 2 different sessionFactories.MyEclipse gives 3 views to manage the hibernate.cfg.xml file, namely configuration view, design view and source view.
Can I use the design view or the source view to add a second session-factory to hibernate.cfg.xml? Is this the correct kind of approach?Else, what is the best way to use MyEclipse to achieve this?
Will I have to do it outside MyEclipse?Your suggestions will be appreciated.
Mike King
Brian FernandesModeratorMike,
I’m afraid you can only have a single Session Factory in your configuration file – this is not a MyEclipse restriction but a hibernate design principle.
I would suggest creating a second configuration file for the connection settings to database2 and switching the configuration file pointed to in your program depending on what you wish to achieve.
Let us know if you require further assistance,
Brian.
danielkohlParticipantHi,
it’s very easy to connect to 2 (or N) databases:
You only have create to configurations:
// Define the locations of the hibernate-configurations private final String LOCAL_CONFIG_FILE_LOCATION = "/local_hibernate.cfg.xml"; private final String WEB_CONFIG_FILE_LOCATION = "/hibernate.cfg.xml"; ........ try { config = new Configuration().configure(configfile); logger.debug("Config: "+config.getProperties()); sessionFactory = config.buildSessionFactory(); logger.debug("Factory: "+sessionFactory.getStatistics()); } catch (Exception excp) { logger.error("Creating configuration for config-file: "+configfile+" failed! Details:"+excp.getMessage()); config = null; sessionFactory = null; } ....... hibernateSession = config.getFactory().openSession(); }
The code isn’t complete here and it on’t work, but i hope you understand the solution 🙂
Greetings
Daniel
MikeKingMemberBrian & Daniel,
Thank you for your assistance. I am just starting to set up 2 configuration files and two sessions that use them.
Mike
-
AuthorPosts