Dynamic mapping

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.

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.

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.

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.

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.

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.

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).

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

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