Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

I am creating a application in which I am receiving the data in JSON format from Jersey RESTServices.I need to dynamically generate the table using the JSON data and also want to store the values in this table. Let us suppose that I get the data as:

     { "fieldLists":
                     {"fields":[

                                {"name":"field_a","type":"any Java Type"},

                                {"name":"field_b","type":"any Java Type"}
                               ]
                     }
      }

Now I need to create table using this data.I am stuck how to create class and its mapping at run time?

Please suggest which approach will be better?

share|improve this question
    
Why would you want to create a new table each time you get a JSON response? Why not just store the JSON value in a json column of a pre-defined table? – a_horse_with_no_name 15 hours ago
    
@a_horse_with_no_name Actually JSON response for table creation and store data will be managed dynamically. The table will be created on the basis of table creation request and its structure is not predefined. – anand mishra 15 hours ago
    
This is not how relational databases work. You don't just create a new table for every request. Why can't you just store the JSON object? – a_horse_with_no_name 15 hours ago
    
you just want to create table or you want to create class and table? – Anoop LL 15 hours ago
    
Then how I handle A table creation request and latter its data which will be going to store in this table ? – anand mishra 15 hours ago

An ORM framework is really not an ideal choice for your requirement primarily because they were never designed to support dynamic domain models. While you may use the native sql features of your ORM framework of choice, it's effectively just a wrapper around JDBC.

Using JDBC, you would simply construct the CREATE TABLE DDL statements based on the JSON provided data when a table should be created and then construct the appropriate statements when you need to add, alter, or remove rows.

share|improve this answer
    
thanks. But In this case what will be the approach for data insertion ? – anand mishra 15 hours ago
    
@anandmishra As I indicated in my answer, you would construct the appropriate INSERT statement based on the column data you receive for a new row and execute it via native SQL. – Naros 15 hours ago
    
Thanks for help.. – anand mishra 15 hours ago

First you need to create class. Save it to some location. Compile it and load to memory. Add class to hibernate config.

programmatically-compile-and-instantiate-a-java-class

create database in Hibernate at runtime

share|improve this answer
    
thanks. But I don't want to create database at runtime I need to add new table whenever new table structure is received and store the data in corresponding tables. – anand mishra 15 hours ago
    
I misunderstood question. I though you want to achieveyour goal in some hackish way ;) Naros answer is of course best way to do what you want. – HuTa 14 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.