Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to map dynamically created table in Hibernate?

Tags:

java

hibernate

I am working on a web application. We are using Hibernate as ORM in our project. Actually, our application creates some tables dynamically based on user selection. The user can select table name, column name and then s/he can import data from a csv file. So my question is: how to map this dynamically created table with Hibernate and Java objects?

like image 890
Silent Warrior Avatar asked Oct 02 '09 04:10

Silent Warrior


People also ask

Can tables be dynamically?

Dynamic tables in Excel are the tables where when a new value is inserted into it. As a result, the table adjusts its size by itself. To create a dynamic table in Excel, we have two different methods: making a table of the data from the table section while another using the offset function.

Does hibernate have mapping?

hibernate mappings are one of the key features of hibernate . they establish the relationship between two database tables as attributes in your model. that allows you to easily navigate the associations in your model and criteria queries.

What is dynamic query in hibernate?

Queries that are created at runtime by the application are called dynamic queries. Dynamic queries are created when we pass a simple JPA compliant query string to the createQuery method of the EntityManager class.


2 Answers

It can be done dynamically, but it's somewhat messy:

You'll need to dynamically alter Hibernate's Configuration object before SessionFactory is built. If you're using Spring, this can be done by overriding postProcessAnnotationConfiguration() method of AnnotationSessionFactoryBean; otherwise you'll just need to do it using your Configuration object prior to invoking buildSessionFactory() on it.

If you need to do this without application restart, you're looking at either rebuilding your SessionFactory (which means your users will have to wait until that's done) or using a separate SessionFactory instance specifically dedicated to your custom classes (which is next to impossible if your custom classes need to reference your built-in classes).

You can get access to class / table mappings via configuration.getMappings(). You will then need to create a new table mapping via Table API and add it to configuration via addTable(). Same thing will have to be done with PersistentClass which represents a class mapping. If you're using the same class to represent multiple entities (e.g. map multiple tables) make sure to use unique entity names for each. You'll have to do this (alter the configuration) on every app restart.

like image 136
ChssPly76 Avatar answered Oct 15 '22 08:10

ChssPly76


How much of the tables are dynamically created? Are the tables similar and you just change the database name?

Here is a discussion of changing the table name: http://www.experts-exchange.com/Programming/Languages/Java/J2EE/Frameworks/Spring/Q_24237099.html

If you are completely building a new table, can you use a view, and just direct people to a view?

Why are you using Hibernate for this, rather than just dynamically creating queries in JDBC?

like image 2
James Black Avatar answered Oct 15 '22 07:10

James Black