2

There's an enterprise application using Java + Hibernate + PostgreSQL. Hibernate is configured via annotations in the Java source code. So far the database schema is fixed, but I faced the problem that it needs to be dynamic:I can receive data from different locations and I have to store these in different tables. This means that I have to create tables run-time. Fortunately, it seems that all of these data coming from the different institutes can have the same schema. But I still don't know how to do that using Hibernate. There are two main problems:

  1. How to tell to Hibernate that many different tables have the same structure? For example the "Patient" class can be mapped to not just the "patient" table, but the "patient_mayo_clinic" table, "patient_northwestern" table, etc. I can feel that this causes ambiguity: how Hibernate knows which table to access when I do operations on the Patient class? It can be any (but only one) of the former listed tables.
  2. How can I dynamically create tables with Hibernate and bind a class to them?

Response to suggestions: Thanks for all of the suggestions. So far all of the answers discouraged the dynamic creation of tables. I'll mark Axel's answer, since it achieves certain goals, and it is a supported solution. More specifically it's called multi-tenancy. Sometimes it's important to know some important phrases which describes our problem (or part of our problem). Here are some links about multi-tenancy:

In real world scenario multi-tenancy also involves the area of isolating the sets of data from each other (also in terms of access and authorization by different credentials) once they are shoved into one table.

4 Answers 4

4

You can't do this with Hibernate.

Why not extend your patient table with an institute column?

This way you'll be able to differentiate, without running into mapping issues.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Axel. I've thought about that, but I'll see if anybody has some other advice.
Well, you have this method: docjar.org/docs/api/org/hibernate/cfg/… , but as the javadoc says: The Configuration is meant only as an initialization-time object. SessionFactorys are immutable and do not retain any association back to the Configuration.
3

I am afraid you can't do this easily in Hibernate. You would have to generate the Java source, compile it, add it to your classpath and load it dynamically with java.reflection package. If that works, which I doubt it, it will be an ugly solution (IMHO).

Have you consider using a schema less database i.e: NoSQL databases or RDF databases. They are much more flexible in terms of what you can store in them , basically things are not tight up against a relational schema.

2 Comments

I see your point. I haven't thought that it requires DB admin rights to crate tables. I'm aware of the NoSQL movement, saw presentations on MongoDB. I haven't heard of RDF databases.
RDF is a data model promoted as a standard by the w3c. It's based on the concept of triples and graphs. Look at this introduction of RDF with the Jena API - a Java API for RDF. jena.sourceforge.net/tutorial/RDF_API
2

In most environments it is not a good idea to create tables dynamically simply because dbas will not give you the rights to create tables in production.

Axel's answer may be right for you. Also look into Inheritance Mapping for Hibernate.

1 Comment

Yes, that would mean that I had to have PatientMayoClinic and PatientNorthWestern child classes in advance, both of them inherit from Patient class. But everything is dynamic. Thanks for the advice though!
1

I agree that its not advisable to create tables dynamically nevertheless it's doable.

Personally i would do as Axel Fontaine proposed but if dynamic tables is a must-have for you I would consider using Partitioning.

PostgreSQL allows you to create ona main table and few child tables (partitions), records are disjunctive between child tables, but every record from any child table is visible in parent table. This means that you can insert rows into any child table you want using just simple insert statement (its not cool but has the same level of complexity as composing and persisting an entity, so its acceptable in your case) and query database using HQL

2 Comments

I wonder how much is this partitioning PostgreSQL specific. It is possible that later I have to add MySQL and Oracle support.
if you're asking for generic solution (DBMS independent) to your "situation" I must say I have never heard of such.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.