facebook

hibernate.cfg.xml and multiple database access

  1. MyEclipse Archived
  2.  > 
  3. Database Tools (DB Explorer, Hibernate, etc.)
Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #286882 Reply

    Douglas M Hurst
    Participant

    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”&gt;

    <!– 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

    #286901 Reply

    Loyal Water
    Member

    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?

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

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

    #286912 Reply

    Douglas M Hurst
    Participant

    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.

    #286914 Reply

    Douglas M Hurst
    Participant

    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.

    #286917 Reply

    Douglas M Hurst
    Participant

    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”&gt;

    <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”&gt;

    <!– 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”&gt;

    <!– 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.”

    #286918 Reply

    Douglas M Hurst
    Participant

    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?

    #286982 Reply

    Riyad Kalla
    Member

    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?

Viewing 7 posts - 1 through 7 (of 7 total)
Reply To: hibernate.cfg.xml and multiple database access

You must be logged in to post in the forum log in