title: Hibernate: how to map one-to-many with another database?
body: <p><br />
  I have 2 classes: User and Message. <br />
  One user can have several messages. <br />
  I.e. this is association one-to-many. <br />
  My users table is in first database and the messages table in the second database. <br />
  How can I map them? <br /> <br /> <br /></p>
  
  <p>My users and messages tables mapping: <br /></p>
  
  <pre><code>&lt;hibernate-mapping&gt;
      &lt;class name="com.example.hibernate.User" table="users" lazy="false"&gt;
      &lt;id name="xId" type="int" column="xid" &gt;
              &lt;generator class="increment"/&gt;   
      &lt;/id&gt;
  
      &lt;set name="messages" inverse="true" table="messages"&gt;
          &lt;key&gt;
              &lt;column name="xsin_id" not-null="true" /&gt;
          &lt;/key&gt;
          &lt;one-to-many class="com.example.hibernate.Message" /&gt;
       &lt;/set&gt;     
  &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  
  
  &lt;hibernate-mapping&gt;
    &lt;class name="com.example.hibernate.Message" table="messages" lazy="false"&gt;
         &lt;id name="id" type="int" column="xmsgbox" &gt;
          &lt;generator class="increment"/&gt;     
         &lt;/id&gt;
  
         &lt;many-to-one name="user" class="com.example.hibernate.User"&gt;
                 &lt;column name="xsin_id" not-null="true" /&gt;
             &lt;/many-to-one&gt;
     &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p><strong>I also have 2 *.cfg.xml files where these classes are mapped.</strong></p>
  
  <p>My test code snipplet:</p>
  
  <pre><code>Session session = HibernateUtilUser.getSession();
  
  String SQL_QUERY ="from User user";
  Query query = session.createQuery(SQL_QUERY);
  User user = null;
  for(Iterator it=query.iterate();it.hasNext();){
      user=(User)it.next();               
      break;
  }
  Set&lt;Message&gt; messages = user.getMessages();
  assertNotNull(messages);
  </code></pre>
  
  <p>I get an error: <strong>Association references unmapped class: com.example.hibernate.Message</strong></p>
  
  <p>P.S. my HibernateUtilUser class:</p>
  
  <pre><code>public class HibernateUtilUser {
  
      private static SessionFactory sf;
      private static Session session;
  
      private HibernateUtil() {}
  
      public static SessionFactory getSessionFactory() {      
          if (sf == null) {
              sf = new Configuration().configure("hibernateuser.cfg.xml").buildSessionFactory();
          }       
          return sf;      
      }
  
      public static Session getSession() {
          if (session == null || session.isOpen() == false) {
              session = getSessionFactory().openSession(); 
          }       
          return session;
      }
  
      public Session openSession() {
          return sf.openSession();
      }
  
      public static void close(){
      if (sf != null)
          sf.close();
          sf = null;
      }
  }
  </code></pre>