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
  • 890
  • 11
  • 28

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
  • 18,021
  • 3
  • 35
  • 68
  • 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
  • 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