title: How can I access data from two databases, in a single entity, using Hibernate?
body: <p>I'm trying to learn how I can access two databases. For this I developed a project using hibernate and created two connection like this.</p>
  
  <p>A connection contains only data of a fictitious student, another contains an identification photo. I imagine a database of files which contains PDFs, images...</p>
  
  <p>The image has a composite primary key as it is divided into rows of bytes.</p>
  
  <p>I can connect individually on each base, but I can not when I try to use them at the same time.</p>
  
  <p>Photo.class</p>
  
  <pre><code>@IdClass(PhotoId.class)
  public class Photo {
     @Id
     @GeneratedValue(strategy = GenerationType.AUTO)
     @Column(name = "id_arquivo")
     private int idArquivo;
  
     @Id
     @Column(name = "item", nullable = false)
     private long item;
  
     @Column(name = "conteudo", nullable = false)
     private byte[] conteudo;
  
     @Column(name = "size", nullable = false)
     private long size;
  ...
  }
  
  class PhotoId implements Serializable {
     private static final long serialVersionUID = 1L;
  
     private int idArquivo;
     private long item;
  ...
  }
  </code></pre>
  
  <p>Person.class</p>
  
  <pre><code>public class Person {
     @Id
     @GeneratedValue(strategy = GenerationType.AUTO)
     @Column(name = "id_pessoa")
     private int idPessoa;
  
     @ManyToOne(fetch = FetchType.EAGER)
     @JoinColumn(name = "id_arquivo_foto", referencedColumnName = "id_arquivo")
     private Photo photo;
  }
  </code></pre>
  
  <p>This works perfectly when I call in DAO.
  I have two "hibernate.cfg" and two hibernate util to connect.</p>
  
  <p>PersonDAO.class</p>
  
  <pre><code>public class PersonDAO{
     private static PersonDAO personDAOInstance;
  
     public static PersonDAO getInstance(){
         if(personDAOInstance == null) {
             personDAOInstance = new PersonDAO();
         }
         return(personDAOInstance);
     }
  
     public Person getPersonById(int id){
         Session session = HibernateUtil.getSessionFactory().openSession();
         Person person = null;
  
         try {
             Query consulta = session.getNamedQuery("Person.buscarPorId");
             consulta.setInteger("id", id);
  
             person = (Person) consulta.uniqueResult();
         } catch(RuntimeException ex) {
             throw ex;
         } finally {
             session.close();
         }
  
         return(person);
     }
  }
  </code></pre>
  
  <p>I have another DAO just for the photo, PhotoDAO.class is the same as PersonDAO.class with the necessary mapping changes.
  I call the object and it always gives error, but when I comment the photo field in Person.class works, when I call Photo.class it works, but never Person.class with photo.</p>
  
  <p>I call it this way:</p>
  
  <pre><code>Person person = personDAO.getPersonById(2);
  </code></pre>
  
  <p>I do not know if I should do </p>
  
  <blockquote>
    <p>private List photo</p>
  </blockquote>
  
  <p>, or it is another problem with the connect mode, since Photo.class is mapped by hibernate_base_arquivos.cfg.xml and Person.class by hibernate.cfg.xml</p>