0

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?

anand mishra
  • 900
  • 1
  • 11
  • 30
  • 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? –  Dec 31 '15 at 08:57
  • @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 Dec 31 '15 at 09:03
  • 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? –  Dec 31 '15 at 09:05
  • you just want to create table or you want to create class and table? – Anoop LL Dec 31 '15 at 09:09
  • Then how I handle A table creation request and latter its data which will be going to store in this table ? – anand mishra Dec 31 '15 at 09:11
  • Also how can I specify the which data belong to which category? – anand mishra Dec 31 '15 at 09:12

2 Answers2

1

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.

Naros
  • 19,928
  • 3
  • 41
  • 71
  • thanks. But In this case what will be the approach for data insertion ? – anand mishra Dec 31 '15 at 09:28
  • 1
    @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 Dec 31 '15 at 09:30
0

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

Community
  • 1
  • 1
HuTa
  • 168
  • 1
  • 8
  • 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 Dec 31 '15 at 09:23
  • 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 Dec 31 '15 at 10:43