MyEclipse Forums
This thread has been locked & this forum has been archived.
Please post new topics to Java EE Development (EJB, JSP, Struts, XDoclet, etc.)
View previous topic Printable version Log in to check your private messages View next topic
Author Message
douglasmhurst
Post subject: hibernate.cfg.xml and multiple database access  PostPosted: Jul 09, 2008 - 02:25 AM
Registered Member
Registered Member


Joined: Sep 30, 2006
Posts: 274

Using the wizard at least, when hibernate.cfg.xml is created (I think I remember it's created when you click on MyEclipse->Add Hibernate Capability.

This is what you end up with in source code (below). What if you also need to connect to say an Oracle database... or DB2.

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="connection.username">netgohur_root</property>
<property name="connection.url">jdbc:mysql://localhost:3306/monitor_test</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="myeclipse.connection.profile">MONITOR_CHASSIS</property>
<property name="connection.password">mysql9tdf</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<mapping resource="com/myeclipse/hibernatespring/MonitorSchedule.hbm.xml" />
</session-factory>
</hibernate-configuration>

I've tried adding another session-factory and giving them different names like...

<session-factory name="monitor">
.
.
</session-factory>
<session-factory name="horse">
.
.
</session-factory>

... but that the IDE doesn't like that. I also tried creating monitor.cfg.xml and horse.cfg.xml and then in the Spring 2 applicationContext.xml like so...

<bean id="hibernateSession"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>file:src/monitor.cfg.xml</value>
</property>
</bean>
<bean id="hibernateSession2"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>file:src/horse.cfg.xml</value>
</property>
</bean>

... but the IDE doesn't like that either. Is this some limitation of Hibernate or is it a limitation of MyEclipse?

Thanks
 
 View user's profile Send private message  
Reply with quote Back to top
support-nipun
Post subject: RE: hibernate.cfg.xml and multiple database access  PostPosted: Jul 09, 2008 - 02:47 PM
Registered Member
Registered Member


Joined: Apr 18, 2007
Posts: 8012

Quote:

I've tried adding another session-factory and giving them different names like...

... but that the IDE doesn't like that.

What error do you get when you use another session-factory?


Quote:

I also tried creating monitor.cfg.xml and horse.cfg.xml a

What error do you get when you create two config files?

_________________
Nipun
MyEclipse Support
 
 View user's profile Send private message Send e-mail  
Reply with quote Back to top
douglasmhurst
Post subject: RE: hibernate.cfg.xml and multiple database access  PostPosted: Jul 09, 2008 - 05:48 PM
Registered Member
Registered Member


Joined: Sep 30, 2006
Posts: 274

For the added session-factory

The content of element type "hibernate-configuration" must match "(session-factory, security?)"
--------------------

For trying to use say first.cfg.xml and second.cfg.xml How is the applicationContext.xml going to resolve which to use?

In my main clase I have

// Load the Spring 2 bean configuration and create a bean factory
beanFactory = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));

which resolves the following in the applicationContext.xml

<bean id="hibernateSession"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>file:src/hibernate.cfg.xml</value>
</property>
</bean>

... it doesn't seem to me, it can resolve more than one of these entries. Since hibernate.cfg.xml doesn't allow 2 session-factory entries, I'm stuck with pointing to one database.
 
 View user's profile Send private message  
Reply with quote Back to top
douglasmhurst
Post subject: RE: hibernate.cfg.xml and multiple database access  PostPosted: Jul 09, 2008 - 05:51 PM
Registered Member
Registered Member


Joined: Sep 30, 2006
Posts: 274

BTW, I've done Google searches until I'm blue in the face and I've not seen a satisfactory answer to this question. I was hoping your experts could shed some light on it. Clearly it seems you ought to be able to have Hibernate sessions that point to a myriad of database in an enterprise application... or even just a web app.
 
 View user's profile Send private message  
Reply with quote Back to top
douglasmhurst
Post subject: RE: hibernate.cfg.xml and multiple database access  PostPosted: Jul 09, 2008 - 06:32 PM
Registered Member
Registered Member


Joined: Sep 30, 2006
Posts: 274

I figured this one out on my own. Basically, you need an applicationContext.xml for each Hibernate connection you're going to make. Here are fragments of mine

applicationContext.xml

<beans>
<bean id="hibernateSession"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>file:src/hibernate.cfg.xml</value>
</property>
</bean>
<bean id="persistenceLayer"
class="com.monitor.hibernatespring.PersistenceLayer"
abstract="false" singleton="true" lazy-init="default"
autowire="default" dependency-check="default">
<property name="monitorScheduleDAO">
<ref bean="MonitorScheduleDAO" />
</property>
</bean>
<bean id="MonitorScheduleDAO"
class="com.monitor.hibernatespring.MonitorScheduleDAO">
<property name="sessionFactory">
<ref bean="hibernateSession" />
</property>
</bean>
</beans>

horseContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
<bean id="hibernateSession"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>file:src/horse.cfg.xml</value>
</property>
</bean>
<bean id="horseLayer"
class="com.monitor.hibernatespring.HorseLayer"
abstract="false" singleton="true" lazy-init="default"
autowire="default" dependency-check="default">
<property name="chartEntryDAO">
<ref bean="ChartEntryDAO" />
</property>
</bean>
<bean id="ChartEntryDAO"
class="com.monitor.hibernatespring.ChartEntryDAO">
<property name="sessionFactory">
<ref bean="hibernateSession" />
</property>
</bean>
</beans>

hibernate.cfg.xml

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="connection.username">netgohur_root</property>
<property name="connection.url">jdbc:mysql://localhost:3306/monitor_test</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="myeclipse.connection.profile">MONITOR_CHASSIS</property>
<property name="connection.password">mysql9tdf</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<mapping resource="com/monitor/hibernatespring/MonitorSchedule.hbm.xml" />
</session-factory>
</hibernate-configuration>

horse.cfg.xml

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="connection.username">netgohur_root</property>
<property name="connection.url">jdbc:mysql://localhost:3306/chart_entry</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="myeclipse.connection.profile">horse_race</property>
<property name="connection.password">mysql9tdf</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<mapping resource="com/monitor/hibernatespring/MonitorSchedule.hbm.xml" />
</session-factory>
</hibernate-configuration>


In my main class ...

// Load the Spring 2 bean configuration and create a bean factory
beanFactory = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));

// Load the Spring 2 bean configuration and create a bean factory
horseFactory = new XmlBeanFactory(new ClassPathResource("horseContext.xml"));

// Create instance of PersistenceLayer (Spring 2)
PersistenceLayer persistenceLayer =
(PersistenceLayer) beanFactory.getBean("persistenceLayer");

// Create instance of HorseLayer (Spring 2)
HorseLayer horseLayer =
(HorseLayer) horseFactory.getBean("horseLayer");

I guess my point is, when you right-click on <project>->MyEclipse->Add Hibernate Capability, you're only getting the possibility of connecting to a single JDBC database generated. It seems there might be some way to add the capability "Add an additional Hibernate connection."
 
 View user's profile Send private message  
Reply with quote Back to top
douglasmhurst
Post subject: RE: hibernate.cfg.xml and multiple database access  PostPosted: Jul 09, 2008 - 06:59 PM
Registered Member
Registered Member


Joined: Sep 30, 2006
Posts: 274

And one last thing. Now that I've created 2 factories, and I want to reengineer ChartEntry to horseContext.xml, I CAN'T.

On the RE dialog, both Spring config file and Factory session id: are drop-down boxes that are not editable. Therefore, I can only regenerate to applicationContext.xml and will then have to remove that entry because it's not germane?
 
 View user's profile Send private message  
Reply with quote Back to top
support-rkalla
Post subject: RE: hibernate.cfg.xml and multiple database access  PostPosted: Jul 10, 2008 - 06:04 PM
Registered Member
Registered Member


Joined: Jan 06, 2004
Posts: 23818

Douglas, did you make sure to add the new Application Context files under your project properties > MyEclipse > Spring so MyEclipse knows those are "spring" files?

_________________
Riyad
MyEclipse Support
 
 View user's profile Send private message Visit poster's website  
Reply with quote Back to top
Display posts from previous:     
Jump to:  
All times are GMT - 6 Hours
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.
View previous topic Printable version Log in to check your private messages View next topic
Powered by PNphpBB2 © 2003-2004 The PNphpBB Group
Credits