-->
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.  [ 7 posts ] 
Author Message
 Post subject: Application with different Datasource????
PostPosted: Sun Oct 11, 2009 9:17 am 
Newbie

Joined: Tue Sep 29, 2009 5:02 am
Posts: 8
Hello together!

how can I use a Application with hibernate and different Datasource????
e.g. Also test.hibernate.cfg.xml
db_start.hibernate.cfg.xml replaced


Code:
....
protected static SessionFactory buildSessionFactory() {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
           SessionFactory sf = null;
           System.out.println("DBHandler.url " + DBHandler.url);
           
           Configuration cfg = new Configuration();
           
           
             
           if (DBHandler.url.equals("jdbc:mysql://10.100.1.10/test")){
              cfg.configure("/test.hibernate.cfg.xml");
                 
           
              cfg.buildSettings();
           } else if (DBHandler.url.equals("jdbc:mysql://10.100.1.10:3306/work")){
              cfg.configure("/work.hibernate.cfg.xml");
           else {
              cfg.configure();
           }
       
            return cfg.buildSessionFactory();
        }
        catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }

....
    }


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>
        <property name="hibernate.bytecode.use_reflection_optimizer">false</property>
        <property name="hibernate.connection.driver_clas">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.password">pw123</property>
        <property name="hibernate.connection.url">jdbc:mysql://10.100.1.10:3306/db_start</property>
        <property name="hibernate.connection.username">user</property>
        <property name="hibernate.current_session_context_class">thread</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
        <mapping resource="de/wantech/mda/ZahlungBelegkopf.hbm.xml" />
        <mapping resource="de/wantech/mda/ReportVerleihStatistik.hbm.xml" />
.....




It is neccasary to build for each DB a own application???


Top
 Profile  
 
 Post subject: Re: Application with different Datasource????
PostPosted: Sun Oct 11, 2009 5:15 pm 
Senior
Senior

Joined: Mon Jul 07, 2008 4:35 pm
Posts: 141
Location: Berlin
Hi donbar,

it's not necessary to build a single application for each case. You should switch completely to programmatic configuration and leave out hibernate.cfg.xml. All configuaration settings can be provided in a java.util.Properties instance that you simply add to your org.hibernate.cfg.Configuration object before you build the session factory.

CU
Froestel

_________________
Have you tried turning it off and on again? [Roy]


Top
 Profile  
 
 Post subject: Re: Application with different Datasource????
PostPosted: Mon Oct 12, 2009 4:05 am 
Newbie

Joined: Tue Sep 29, 2009 5:02 am
Posts: 8
hello Froestel,

first, thank you very much for your answer!
But I forgot to mention that I use Reverse Engineering Classes...
sorry...
For the reverse I put one DB into the hibernateConfiguration...
and each xxx.hbm.xml has its catalog...

And I still using this!

Still have this Exception...

Code:
12.10.2009 09:39:18 org.slf4j.impl.JCLLoggerAdapter info
INFO: Not binding factory to JNDI, no JNDI name configured
org.hibernate.MappingException: Unknown entity: de.wantech.mda.Artikelgruppe
   at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:628)
   at org.hibernate.impl.SessionImpl.getEntityPersister(SessionImpl.java:1366)
   at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:87)
   at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:906)
   at org.hibernate.impl.SessionImpl.load(SessionImpl.java:812)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:597)
   at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:342)
   at $Proxy0.load(Unknown Source)
   at sadrach.plugin.stammdaten.artikelgruppe.ArtikelGruppeDBObject.loadObject(ArtikelGruppeDBObject.java:42)



Any more ideas???


Top
 Profile  
 
 Post subject: Re: Application with different Datasource????
PostPosted: Mon Oct 12, 2009 9:24 am 
Newbie

Joined: Tue Sep 29, 2009 5:02 am
Posts: 8
Now I try this...

Code:
   protected static SessionFactory buildSessionFactory() {
        try {
                       
           Configuration cfg = new Configuration();
                      
             
           if (DBHandler.url.equals("jdbc:mysql://10.100.1.116:3306/fischer_test")){

              cfg.setProperty("hibernate.connection.url", "jdbc:mysql://10.100.1.116:3306/fischer_test");              
              cfg.setProperty("hibernate.default_catalog", "fischer_test");
              cfg.setProperty("hibernate.default_schema", "fischer_test");          
              cfg.addClass(de.wantech.mda.Artikelgruppe.class);
              
              System.out.println("config-prop: " +  cfg.getProperties());
              
              
           } else if (DBHandler.url.equals("jdbc:mysql://10.100.1.116:3306/wantech_test")){
              

              cfg.setProperty("hibernate.connection.url", "jdbc:mysql://10.100.1.116:3306/wantech_test");
              cfg.setProperty("hibernate.default_catalog", "wantech_test");
              cfg.setProperty("hibernate.default_schema", "wantech_test");
                            
              System.out.println("config-prop: " +  System.getProperties());
              

           }
           else {
              cfg.configure();
           }
       
            return cfg.buildSessionFactory();
        }
        catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }



but still it throws a org.hibernate.MappingException: Unknown entity: de.wantech.mda.Artikelgruppe


Top
 Profile  
 
 Post subject: Re: Application with different Datasource????
PostPosted: Mon Oct 12, 2009 11:32 am 
Senior
Senior

Joined: Mon Jul 07, 2008 4:35 pm
Posts: 141
Location: Berlin
Hi donbar,

ok, so the application is missing an entity named
Code:
de.wantech.mda.Artikelgruppe
which is also the class name of the corresponding class I suppose. If you do reverse engineering, is this class created? If not so, is there a, probably similarly named class, created you did not expect.

Maybe, it's just a typo. Watch the case since Hibernate is case sensitive. Did you include all resources you require to the reverse engineering configuration?

CU
Froestel

_________________
Have you tried turning it off and on again? [Roy]


Top
 Profile  
 
 Post subject: Re: Application with different Datasource????
PostPosted: Tue Oct 13, 2009 8:12 am 
Newbie

Joined: Tue Sep 29, 2009 5:02 am
Posts: 8
thanks!

but sorry

Quote:
the application is missing an entity named
Code:
de.wantech.mda.Artikelgruppe


because of deleting the attribute (catalog="db_start")
from Artikelgruppe.hbm.xml

...
First the application works with only db_start.
But changing the db is impossible for me!

????


Top
 Profile  
 
 Post subject: Re: Application with different Datasource????
PostPosted: Thu Oct 15, 2009 5:23 am 
Newbie

Joined: Tue Sep 29, 2009 5:02 am
Posts: 8
thank you for help!

I solved the problem

The default_catalog was crucial!
It provides for no entry in the xxx.hbm.xml.


Code:
....
<hibernate-configuration>
    <session-factory>
.....
   <property name="hibernate.default_catalog">dbv_test</property>
....



the switch work like this

Code:
         Configuration cfg = new Configuration();

         cfg.configure("/" + DBHandler.db + ".hibernate.cfg.xml");

         return cfg.buildSessionFactory();


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.