I have an entity which retrieves its data from various tables according to how I call this entity, it is shown here : hibernate two tables per one entity which works fine for me. But how can set hibernate to call various Databases for one entity ?
Asked
Active
Viewed 431 times
1
-
1Please describe your need so that specific answer should be given. – Amogh Jul 28 '15 at 19:22
-
@Amogh .I look for an answer which is similar to this I've linked , if that's possible , otherwise I am open to new suggestions. The idea is while my java program is running depending on the input it gets it will connect the x entity-class to the desired table x of the y Database .To keep things simple suppose I have 3 entity variants and each of them has its data to another DB but still keeping the same schema. – Phil_Charly Jul 28 '15 at 19:51
-
1For that you have to define multiple `sessionFactory` (datasource) take a look at http://www.javabeat.net/configuring-multiple-databases-in-hibernate/ or in spring you can dynamically change data source : http://stackoverflow.com/questions/13507522/dynamically-change-spring-data-source – Amogh Jul 28 '15 at 20:04
-
@Amogh .Thank you for your helpful feedback, I think I can cope with that. No meaning to tire you I have another question, I was told that this can be done with the discriminator value, is that possible ?and again thank you for those links . – Phil_Charly Jul 28 '15 at 20:40
-
Explanation is added as answer as comments are being so chatty, I hope that explanation will clear all the things but still feel free to ask me anything and also correct me if anywhere I am wrong – Amogh Jul 28 '15 at 20:59
-
Can you share us your implementation? I have the same situation. Say, there is an entity Student and two databases: database A (H2 in-memory) and database B (postgres). Both of them have students table and I don't want to create different entity for each – valijon Apr 03 '18 at 05:12
1 Answers
2
According to your need where you want use multiple DBs to work on single entity. (correct me again if I am wrong)
So to work with multiple DBs in hibernate please refer links 1 and 2 as it's asked son many times on SO.
About discriminator value (discriminator-value
), I don't think it's fully possible because according to Docs 3 discriminator used for inheritance purpose.
What I understand is if you have 2 DBs having Table Student
with two joined classes BadStudent
and GoodStudent
but which database to use for work is only decided by hibernate session factory which is configured by a datasource(DB), So if you want to work on 2 DBs you have to define two datasources.
Correct me If I misunderstood your requirement or doubt.
-
You 're right ,still I 'll correct you that in my case I don't have subclasses, it's like I linked my link above, but I don't think it changes the `session factory` plan. Thnx you for your comprehensive reply (y). – Phil_Charly Jul 28 '15 at 21:03
-
1