# Dynamic mapping

**URL:** <https://discourse.hibernate.org/t/dynamic-mapping/738>\
**Category:** Hibernate ORM\
**Created:** [May 10, 2018, 3:13pm UTC](https://discourse.hibernate.org/t/dynamic-mapping/738 "2018-05-10T15:13:02Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ChesterTheJester](https://avatars.discourse-cdn.com/v4/letter/c/49beb7/32.png) [@ChesterTheJester](https://discourse.hibernate.org/u/ChesterTheJester)\
**Post date:** [May 10, 2018, 3:13pm UTC](https://discourse.hibernate.org/t/dynamic-mapping/738/1 "2018-05-10T15:13:02Z")

</div>

I have a program that uses hibernate for database connectivity. I’m looking for way to add mapping files to a session factory dynamically I am currently doing it wrong(so wrong it’s ridiculous) but we have to be able to connect to any database anywhere and any table with out being like please type in your schema. We get entity names and column names. This is possible and The best I can do off the top of my head is to recreate the session factory per mapping request. I was hoping some one could do better than me. If you have any question just ask I’ll be staring at this for days.

---

<div class="post-metadata">

**Author:** ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)\
**Post date:** [May 10, 2018, 5:07pm UTC](https://discourse.hibernate.org/t/dynamic-mapping/738/2 "2018-05-10T17:07:52Z")

</div>

Hibernate supports mapping entities to existing tables. In order to support dynamic mapping, you’d have to recreate the `SessionFactory` at runtime with the new mappings.

---

<div class="post-metadata">

**Author:** ![ChesterTheJester](https://avatars.discourse-cdn.com/v4/letter/c/49beb7/32.png) [@ChesterTheJester](https://discourse.hibernate.org/u/ChesterTheJester)\
**Post date:** [May 10, 2018, 5:16pm UTC](https://discourse.hibernate.org/t/dynamic-mapping/738/3 "2018-05-10T17:16:37Z")

</div>

yes vlad And I’m willing to do this… I’m just trying to come up with a way to deal with mappings so that I don’t end up with 100 session factories connected to the same database because of the mappings. I think rewriting the mapping files if they exist and adding new columns and only restarting the session factories on the rewrites.

---

<div class="post-metadata">

**Author:** ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)\
**Post date:** [May 10, 2018, 5:35pm UTC](https://discourse.hibernate.org/t/dynamic-mapping/738/4 "2018-05-10T17:35:57Z")

</div>

Hibernate is suitable if the schema evolves with time, not if the schema is completely different from one execution to the other.

In your case, maybe JPA and Hibernate are not the right choice.

---

<div class="post-metadata">

**Author:** ![ChesterTheJester](https://avatars.discourse-cdn.com/v4/letter/c/49beb7/32.png) [@ChesterTheJester](https://discourse.hibernate.org/u/ChesterTheJester)\
**Post date:** [May 10, 2018, 6:21pm UTC](https://discourse.hibernate.org/t/dynamic-mapping/738/5 "2018-05-10T18:21:29Z")

</div>

They are absolutely the wrong choices… but I didn’t design it and I’m not allowed to tear out the libraries and go jdbc on it. So I’m trying to optimize this the best I can. We all have our bosses. and all the bosses are crazy

And thank you sooooooo much for the above statement… I am pointing to that right now while I rip our design apart.

---

<div class="post-metadata">

**Author:** ![ChesterTheJester](https://avatars.discourse-cdn.com/v4/letter/c/49beb7/32.png) [@ChesterTheJester](https://discourse.hibernate.org/u/ChesterTheJester)\
**Post date:** [May 10, 2018, 6:29pm UTC](https://discourse.hibernate.org/t/dynamic-mapping/738/6 "2018-05-10T18:29:51Z")

</div>

And just to reiterate your point vlad by our design right now… We have a session factory leak. But also we do a bunch of work just to work around the idea of mapping an object… not use it. It so ridiculous I can’t even begin to know where to start. But at some point I’ve figure out a customer wanted a solution that used hibernate. So we built GOD knows how many class to accommodate this request and Now I’m trying to fix it and I’m like if we are going to do it this way I should at least try and do it right and not create a session factory for every database request/transaction. And now I’m just whining. Sorry But know one else will listen to my tales of sorrow.

---

<div class="post-metadata">

**Author:** ![p3consulting](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/p3consulting/32/565_2.png) [@p3consulting](https://discourse.hibernate.org/u/p3consulting)\
**Post date:** [May 11, 2018, 7:44am UTC](https://discourse.hibernate.org/t/dynamic-mapping/738/7 "2018-05-11T07:44:11Z")

</div>

You could keep Hibernate and JPA to access a meta-data database where you will cache the information to access the actual database that will be targeted, avoiding costly reloading, parsing of meta-data, and the dynamic SQL generation to be done several time if the target database meta-data has not changed.  
But the actual data itself should be accessed through JDBC (using the queries you save in the meta-data “cache” database).

---

<div class="post-metadata">

**Author:** ![ChesterTheJester](https://avatars.discourse-cdn.com/v4/letter/c/49beb7/32.png) [@ChesterTheJester](https://discourse.hibernate.org/u/ChesterTheJester)\
**Post date:** [May 11, 2018, 7:04pm UTC](https://discourse.hibernate.org/t/dynamic-mapping/738/8 "2018-05-11T19:04:02Z")

</div>

Thats sorta clever. I like the idea. I just did a dirty solution for the session factory leak.

---

<div class="post-metadata">

**Author:** ![p3consulting](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/p3consulting/32/565_2.png) [@p3consulting](https://discourse.hibernate.org/u/p3consulting)\
**Post date:** [May 12, 2018, 7:59am UTC](https://discourse.hibernate.org/t/dynamic-mapping/738/9 "2018-05-12T07:59:17Z")

</div>

It could also help to implement authorities management: which user can access to which database/table/column.
