Reputation: 1541
The situation is the following: I have two databases Db1 and Db2, for which I have two EntityManagers em1 and em2 defined. Furthermore, I have the entity Person(int id, String name, Pet pet) mapped to the table persons(id, name) in the database Db1 and the entity Pet(int id, String name, Person owner) mapped to the table pets(id, name, person_id) in the database db2. The relation between Person and Pet is a @OneToOne realtion. At some point in the program, I would like to do something like this:
PersonDAO personDAO = db1DAOFactory.getPersonDAO();
Person person = personDAO.find(100);
System.out.println(person.getPet().getName());
There is no possibility to merge both databases. How can I tell Hibernate to use for the pet field the EntityManager em2? With Hibernate I use only annotations, no xml configuration.
Thank you very much!
Upvotes: 0
Views: 413
Reputation: 117
Sorry but I think you can't. The question is answered by nakosspy:
Hibernate will create an SQL query for your HQL or Criteria query and this SQL will be sent through jdbc to the database. This means that hibernate does not support for what you are trying to do.
However, you can achieve the same result in some cases. Some databases give you the option to create an alias for a table that resides in a different database. So you will be able to write an SQL query that joins the two tables and execute it on the database.
We are doing that with DB2. If you can do that, it depends on your database.
I guess, that it would impossible if you have two different databases (example DB2 and MySQL) but if both databases are of the same vendor, then maybe it's achievable.
You should try to find more info in you database server's documentation.
If you want to see his original post check this link.
Upvotes: 2
Reputation: 4239
This is dependent on database features. If you are using Oracle database, You could achieve this by creating db link in database then map these entity through creating a view or a synonym in oracle database. Check your database features. Hope this helps.
Upvotes: 1