Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 14 posts ] 
Author Message
 Post subject: Multiple database configuration
PostPosted: Thu Jan 12, 2006 5:08 am 
Newbie

Joined: Wed Nov 30, 2005 10:54 am
Posts: 13
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.1

Name and version of the database you are using: Oracle 9i2


Hi,

I would like some help (example configuration files, opinions and suggestions) whilst configuring Hibernate to use serveral databases. The current application being developped using Spring / Java and Hibernate must access 3 databases.

How should I configure the hibernate configuration file ? How will my HibernateUtil.java be impacted ?

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 12, 2006 6:07 am 
Expert
Expert

Joined: Thu May 26, 2005 9:19 am
Posts: 262
Location: Chennai, India
Here we go,

http://www.mail-archive.com/hibernate-d ... 00834.html

_________________
RamnathN
Senior Software Engineer
http://www.linkedin.com/in/ramnathn
Don't forget to rate.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 12, 2006 6:22 am 
Newbie

Joined: Wed Nov 30, 2005 10:54 am
Posts: 13
Hi thanks for your reply, but i'm not to sure that it works.

Heres my hibernate.cfg.xml :

Code:
<?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">

<hibernate-configuration>
    <session-factory >

      <!-- local connection properties -->
      <property name="hibernate.connection.url">jdbc:mysql://url/name</property>
      <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
      <property name="hibernate.connection.username">username</property>
      <property name="hibernate.connection.password">password</property>
      <!-- property name="hibernate.connection.pool_size"></property -->

      <!-- dialect for MySQL -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

        <property name="hibernate.show_sql">false</property>
        <property name="hibernate.use_outer_join">true</property>
        <property name="hibernate.cglib.use_reflection_optimizer">false</property>

      <mapping resource="accesdonnees/Etablissement.hbm" />
      <mapping resource="accesdonnees/Composante.hbm" />
      <mapping resource="accesdonnees/Intervenant.hbm" />
      <mapping resource="accesdonnees/TypeMatiere.hbm" />
      <mapping resource="accesdonnees/Voeu.hbm" />
      <mapping resource="accesdonnees/Service.hbm" />

    </session-factory>
   
    <session-factory >

      <!-- local connection properties -->
      <property name="hibernate.connection.url">jdbc:oracle:thin:@url:1521:name</property>
      <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
      <property name="hibernate.connection.username">username</property>
      <property name="hibernate.connection.password">password</property>
      <!-- property name="hibernate.connection.pool_size"></property -->

      <!-- dialect for Oracle 9 -->
        <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>

        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.use_outer_join">true</property>

      <mapping resource="Person.hbm" />

    </session-factory>
   
</hibernate-configuration>


Both configurations work when used on their own, but when put together I get the following error :

Quote:
Exception in thread "main" java.lang.ExceptionInInitializerError
at cri.univrennes1.fr.accesdonnees.services.PersonneService.createPersonne(PersonneService.java:23)
at cri.univrennes1.fr.hibernate.TestClient.main(TestClient.java:36)
Caused by: java.lang.RuntimeException: Problème de configuration : problem parsing configuration/hibernate.cfg.xml
at cri.univrennes1.fr.hibernate.HibernateUtil.<clinit>(HibernateUtil.java:18)
... 2 more
Caused by: org.hibernate.HibernateException: problem parsing configuration/hibernate.cfg.xml
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1173)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1112)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1098)
at cri.univrennes1.fr.hibernate.HibernateUtil.<clinit>(HibernateUtil.java:16)
... 2 more
Caused by: org.hibernate.MappingException: invalid configuration
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1169)
... 5 more
Caused by: org.xml.sax.SAXParseException: The content of element type "hibernate-configuration" must match "(session-factory,security?)".
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:236)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:172)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:382)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:316)
at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.handleEndElement(XMLDTDValidator.java:2048)
at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.endElement(XMLDTDValidator.java:932)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanEndElement(XMLNSDocumentScannerImpl.java:719)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(XMLDocumentFragmentScannerImpl.java:1685)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:368)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:834)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:764)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:148)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1242)
at org.dom4j.io.SAXReader.read(SAXReader.java:339)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1168)
... 5 more


In the official hibernate documentation, I found the following :

Quote:
Note that this XML configuration uses a different DTD. We configure Hibernate's SessionFactory - a global factory responsible for a particular database. If you have several databases, use several <session-factory> configurations, usually in several configuration files (for easier startup).


Has anybody got any ideas ?

Thanks again.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 12, 2006 6:31 am 
Regular
Regular

Joined: Tue Jan 03, 2006 9:20 am
Posts: 74
You've not supplied a poolsize. Not sure if that's allowed, maybe you need to supply a number there?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 12, 2006 6:42 am 
Newbie

Joined: Wed Nov 30, 2005 10:54 am
Posts: 13
Thanks jwenting for your reply, but it doesn't resolve the problem.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 12, 2006 7:11 am 
Senior
Senior

Joined: Tue Aug 23, 2005 8:52 am
Posts: 181
You need 2 separate hibernate.xml files , one each for session-factory. The DTD says it all
"The content of element type "hibernate-configuration" must match "(session-factory,security?)
"
That is , a hibernate-configuration can have only one session-factory and one or zero security tags.
Also, if you have multiple sessionFactories in a single file, how you would construct your SessionFactory Object? Which one should hibernate give it for you?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 12, 2006 7:17 am 
Newbie

Joined: Wed Nov 30, 2005 10:54 am
Posts: 13
Hi rajasaur, thanks for your reply.

What should the names of my configuration files be ? And how should I change my HibernateUtil.java ?

Thanks again.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 12, 2006 7:23 am 
Senior
Senior

Joined: Tue Aug 23, 2005 8:52 am
Posts: 181
You can name the xml files whatever you want. hibernate.cfg.xml is the default name that it looks up, but you can have two files, say, hib1.cfg.xml and hib2.cfg.xml and configure your SessionFactories as
Code:
SessionFactory sf1= new Configuration().configure("hib1.cfg.xml").buildSessionFactory();
SessionFactory sf2= new Configuration().configure("hib2.cfg.xml").buildSessionFactory();


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 12, 2006 7:43 am 
Expert
Expert

Joined: Thu May 26, 2005 9:19 am
Posts: 262
Location: Chennai, India
So, Cant we have two sessionFactory in a single cfg file?.

Will look in this in detail.

_________________
RamnathN
Senior Software Engineer
http://www.linkedin.com/in/ramnathn
Don't forget to rate.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 12, 2006 7:56 am 
Newbie

Joined: Wed Nov 30, 2005 10:54 am
Posts: 13
Thanks rajasaur, it works.

Does that mean that I have to call the correct sessionFactory when selecting objetcs from each database ?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 12, 2006 8:01 am 
Expert
Expert

Joined: Thu May 26, 2005 9:19 am
Posts: 262
Location: Chennai, India
Yes you have to call the Correct SessionFactory respectively.

_________________
RamnathN
Senior Software Engineer
http://www.linkedin.com/in/ramnathn
Don't forget to rate.


Top
 Profile  
 
 Post subject: ClassCastException w/ 2 dbs
PostPosted: Wed Feb 22, 2006 6:21 pm 
Newbie

Joined: Thu Feb 16, 2006 4:45 pm
Posts: 5
Hi all,

I tried configuring my application to have multiple database access by following the above suggestions, but, was unsuccessful.

I'm using a HibernateUtil class to configure each session. And am doing this at the same time:
Code:
    private static final SessionFactory firstFactory, secondFactory;
    private static Logger logger = Logger.getLogger(HibernateUtil.class.getName());
    static {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            firstFactory = new Configuration().configure("first.cfg.xml").buildSessionFactory();
           secondFactory = new Configuration().configure("second.cfg.xml").buildSessionFactory();
        } catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            logger.error("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getFirstFactory() {
        return firstFactory;
    }   
    public static SessionFactory getSecondFactory() {
        return secondFactory;
    }   


The static block finishes fine for the first one, but, not the second.

There are similar table names in each db and was wondering if this might be the problem. I've given each model class differing package names, but, still get the ExceptionInInitializerError.

Code:
Caused by: java.lang.ExceptionInInitializerError
   at com.util.HibernateUtil.<clinit>(Unknown Source)
   at com.delegate.hbm.HibAnnualExamDel.getAnnualExam(Unknown Source)
   at com.action.AdminExamAction.showExamDetail(Unknown Source)
   ... 49 more
Caused by: java.lang.ClassCastException
   at org.hibernate.tuple.PropertyFactory.buildVersionProperty(PropertyFactory.java:83)
   at org.hibernate.tuple.EntityMetamodel.<init>(EntityMetamodel.java:157)
   at org.hibernate.persister.entity.AbstractEntityPersister.<init>(AbstractEntityPersister.java:412)
   at org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:108)
   at org.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.java:55)
   at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:215)
   at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1176)
   ... 52 more


Also, I'm attempting to use the firstFactory first. By initializing them at the same time would I somehow be referencing the second one even though I explicitly call getFirstFactory?

Code:
        Session session = HibernateUtil.getFirstFactory().getCurrentSession();
        session.beginTransaction();   
       // doesn't look to get past this point
com.model.hbm.sp.AnnualExam annualExam = (com.model.hbm.sp.AnnualExam) session
                .createQuery("select a from AnnualExam a where a.id = :aid")
                .setParameter("aid", new       Integer(annualExamId)).uniqueResult();   
       
        // find the questions
        Query q = session.createQuery(
           "from AnnualExamQuestion aeq where aeq.annualExam = :ae");
        List questions = q.setParameter("ae", annualExam).list();       
           annualExam.setAnnualExamQuestions(Collections.synchronizedSet(new            HashSet(questions)));         
               
        session.getTransaction().commit();       


TIA - g


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 22, 2006 6:30 pm 
Newbie

Joined: Thu Feb 16, 2006 4:45 pm
Posts: 5
<strike>sorry, looks like i accidentally imported a wrong class. :)</strike>

a little presumptuous. still getting the same error.

anyone?

Code:
Caused by: java.lang.ClassCastException
   at org.hibernate.tuple.PropertyFactory.buildVersionProperty(PropertyFactory.java:83)
   at org.hibernate.tuple.EntityMetamodel.<init>(EntityMetamodel.java:157)
   at org.hibernate.persister.entity.AbstractEntityPersister.<init>(AbstractEntityPersister.java:412)
   at org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:108)
   at org.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.java:55)
   at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:215)
   at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1176)
   ... 52 more


Thanks again.


Top
 Profile  
 
 Post subject: Re: Multiple database configuration
PostPosted: Sat Mar 31, 2012 8:02 am 
Newbie

Joined: Sat Jul 30, 2011 3:14 am
Posts: 5
Thanks rajasaur its working fine for me.
I have two separate config file in for two diff databases and I am using two diff "SessionFactory" object for this like -

Code:
SessionFactory sf1= new Configuration().configure("hib1.cfg.xml").buildSessionFactory();
SessionFactory sf2= new Configuration().configure("hib2.cfg.xml").buildSessionFactory();


and its working fine.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 14 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.