title: Creating tables dynamically in Hibernate environment
body: <p>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:</p>
  
  <ol>
  <li>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.</li>
  <li>How can I dynamically create tables with Hibernate and bind a class to them?</li>
  </ol>
  
  <hr>
  
  <p>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:</p>
  
  <ul>
  <li><a href="https://planet.jboss.org/post/multi_tenancy_in_hibernate" rel="nofollow">Multi-tenancy in Hibernate</a></li>
  <li><a href="https://docs.jboss.org/hibernate/orm/4.1/devguide/en-US/html/ch16.html" rel="nofollow">Hibernate Chapter 16. Multi-tenancy</a></li>
  <li><a href="https://community.jboss.org/wiki/Multi-tenancyDesign" rel="nofollow">Multi-tenancy Design</a></li>
  <li><a href="http://wiki.eclipse.org/EclipseLink/Examples/JPA/Multitenant" rel="nofollow">EclipseLink JPA multi-tenancy</a></li>
  </ul>
  
  <p>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.</p>