-->
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.  [ 8 posts ] 
Author Message
 Post subject: Multiple Databases How To
PostPosted: Tue Mar 15, 2005 5:38 pm 
Regular
Regular

Joined: Wed Jan 19, 2005 4:52 pm
Posts: 61
Location: Kansas City, USA
Ok, I am sure this has been answered before, but I cannot find it in the serch of the fourms, but how do I connect/map to 2 different databases. They are both on the same server.

Tim


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 15, 2005 5:41 pm 
Pro
Pro

Joined: Tue Aug 26, 2003 1:24 pm
Posts: 213
Location: Richardson, TX
Use the versions of SessionFactory.openSession() that take a Connection object. Produce the connections yourself.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 15, 2005 6:21 pm 
Regular
Regular

Joined: Wed Jan 19, 2005 4:52 pm
Posts: 61
Location: Kansas City, USA
what about my hibernate.cfg.xml file, do I put the all the mappings in the same session-factory node?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 15, 2005 6:35 pm 
Pro
Pro

Joined: Tue Aug 26, 2003 1:24 pm
Posts: 213
Location: Richardson, TX
You won't be able to put any connection info into your cfg.xml file, I believe. You'll have to get your connection info through other means.

You could have multiple SessionFactory instances with seperate cfg.xml files as well. (Not the default hibernate.cfg.xml)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 24, 2005 3:46 pm 
Regular
Regular

Joined: Wed Jan 19, 2005 4:52 pm
Posts: 61
Location: Kansas City, USA
Ok, I have created 2 cfg.xml files, the first is named xops_hibernate.cfg.xml the next is nations_hibernate.cfg.xml the code in each is as follows:

Code:

<hibernate-configuration>


   <session-factory name="java:hibernate/Nations">
        <!-- properties -->
        <property name="connection.username">user</property>
        <property name="connection.url">jdbc:microsoft:sqlserver://sqldev:1433;DatabaseName=nations_robin</property>
        <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
        <property name="connection.password">pass</property>
        <property name="connection.driver_class">com.microsoft.jdbc.sqlserver.SQLServerDriver</property>

        <!-- mapping files -->
        <mapping resource="com/nationsholding/xOps/common/model/OrderNumber.hbm.xml"/>

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




and
Code:
<hibernate-configuration>

    <session-factory name="java:hibernate/xOps">
        <!-- properties -->
        <property name="connection.username">user</property>
        <property name="connection.url">jdbc:microsoft:sqlserver://sqldev:1433;DatabaseName=xOps</property>
        <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
        <property name="connection.password">pass</property>
        <property name="connection.driver_class">com.microsoft.jdbc.sqlserver.SQLServerDriver</property>

        <!-- mapping files -->
        <mapping resource="com/nationsholding/xOps/common/model/ApplicationProducts.hbm.xml"/>
        <mapping resource="com/nationsholding/xOps/common/model/CustomerOrder.hbm.xml"/>
        <mapping resource="com/nationsholding/xOps/common/model/EntityType.hbm.xml"/>
        <mapping resource="com/nationsholding/xOps/common/model/OrderActivty.hbm.xml"/>
        <mapping resource="com/nationsholding/xOps/common/model/SubProducts.hbm.xml"/>
        <mapping resource="com/nationsholding/xOps/common/model/XopsCustomer.hbm.xml"/>

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


I have a HibernateUtil class that gets the session factory:

Code:
public class HibernateUtil {

    private static final Log LOG = LogFactory.getLog(HibernateUtil.class);
   
   private static final ThreadLocal threadSession = new ThreadLocal();
   private static final ThreadLocal threadTransaction = new ThreadLocal();
   
   static{
      try{         
         new Configuration().configure("xops_hibernate.cfg.xml").buildSessionFactory();
      }catch (Throwable ex){
         LOG.error(ex);
         throw new ExceptionInInitializerError(ex);
      }
      
   }
   
   public static SessionFactory getSessionFactory(){
      SessionFactory sessions = null;
      try{
         Context ctx = new InitialContext();
         String jndiName = "hibernate/xOps";
         sessions = (SessionFactory)ctx.lookup(jndiName);
      }catch(NamingException ne){
         LOG.error(ne);
      }
      return sessions;
   }
   

......



When I try to get a session factory I get this error message.



Hibernate 3.0rc1
- hibernate.properties not found
- using CGLIB reflection optimizer
- using JDK 1.4 java.sql.Timestamp handling
- configuring from resource: /hibernate.cfg.xml
- Configuration resource: /hibernate.cfg.xml
- /hibernate.cfg.xml not found
- org.hibernate.HibernateException: /hibernate.cfg.xml not found

I dont understand why I am getting this. In my Util class I have told hibernate to use a different cfg.xml. Can someone please help me?

Thanks
Tim


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 25, 2005 1:27 pm 
Regular
Regular

Joined: Wed Jan 19, 2005 4:52 pm
Posts: 61
Location: Kansas City, USA
whew, looks like I am blazing a new trail. Ok I figured it out. Here is what I ended up doing:

Created seperate classes to create the session factories for each cfg.xml file. I only create the factories when I need them this way. Also came across a bug, I think it is a JRun issue so I am trying to deploy to JBoss to see if that makes a difference but anyway the bug was that I was calling the Configuration().configure(String) method but it looked like is was still going to the Configuration.configure() method. Kinda wierd but I dont know. So I ended up using the Configuration().configure(Document) method, this worked fine. If anyone needs a more detailed solution to this let me know, I would be more than happy to write it up. Thanks for the help.

Tim


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 28, 2005 10:04 am 
Newbie

Joined: Mon Mar 28, 2005 10:00 am
Posts: 1
If you would not mind writing it up, I would greatly appreciate it.

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 01, 2007 4:18 pm 
Newbie

Joined: Thu Feb 01, 2007 4:12 pm
Posts: 2
Location: New Jersey
May I receive a copy of the writeup?

Thank you in advance.


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