5

my case is the following. I have a JSF form with three outputtexts & the corresponding inputtexts lets say:

outputtext1 - inputtext1

outputtext2 - inputtext2

outputtext3 - inputtext3

Currently i use a backbean method 'Save' in order to store them into the database (hibernate object lets say table1 with primary key table1.id) into fields table1.field1, table1.field2, table1.field3.

So each record in the table have the values inserted in the inputtexts.

My question is how am i going to store form data in the database, in a form like the following:

{"outputtext1:inputtext1","outputtext2:inputtext2"."outputtext3:inputtext3"}

and then fetch them again, parse and rebuild the form. I am thinking of manipulating form data as JSON object... But i am new to both Java+JSON so some guideliness would be really useful for me!

This is an indicative example, form are going to by dynamic and created on the fly.

Kukeltje
12.4k4 gold badges26 silver badges47 bronze badges
asked Apr 5, 2013 at 14:23
3
  • It's just a String, use varchar. Use a JSON Serializer. Commented Apr 5, 2013 at 14:24
  • so am i in the right direction on how to manipulate form data? The form is going to be dynamic so there is no standard format of form data. Commented Apr 5, 2013 at 14:38
  • If there is no standard form, then I'm assuming you are generating the JSON String. Once you have it, store it in a varchar field. Commented Apr 5, 2013 at 14:46

4 Answers 4

4

Why would you want to serialize/deserialize a JSON to send it directly to the database? Deserialization has its own issues and multiple deserializations could (not will) be a source of issues.

You should save the fields as attributes from a given entity and then, with the help of libraries like Gson, generate the JSON from the entity.


Update

Since your form is dynamic, you can use some adaptable entity structure to hold your data.

Your entities can either have a Map<String,String> attribute or a collection of, say, FieldRecord entities that contain a key - value pair.

I suggest this because a JSON in the database can lead to complex issues, in particular if you'll have to query that data later. YOu'll have to process the JSONs in order to either report or find which records have a particular field. This is just an example, things can get more complex.

answered Apr 5, 2013 at 14:31
Sign up to request clarification or add additional context in comments.

4 Comments

The form is going to be dynamic, so i am not going to have a standard number or type of input fields. This is just an indicative example!
@thanili You can have an entity with a Map<String,String> or a list of association entities. IMO Storing a JSON in the database can be very difficult to handle, in particular if you want to query about particular fields later. Still, if you're set on using a json, just create it and store it into a VARCHAR variable.
OK Gamb, i will check your suggestion for a simpler way. Indeed i would have to query the database in order to fetch already submitted values for forms that are re-loaded. So i guess i will have to fetch JSON object, deserialize it, parse it, compare it with form field ids and recostruct the form :) it sounds too much trouble...
@thanili That's exactly what I wanted to spare you :)
2

Simple, you need a BLOB type column in your table to store the json and when you retrieve it in java you just need to decode the json and i recomend using https://code.google.com/p/json-simple/ its very simple

answered Apr 5, 2013 at 14:27

Comments

1

Convert JSONObject into a Stringform and then store. And when your read it back, convert it back into JSONObject like below :

 JSONObject obj = new JSONObject(stringRepresentationOfJSON);
answered Apr 5, 2013 at 14:32

Comments

-2

change hibernate-mapping like this JSONObject obj = new JSONObject(stringRepresentationOfJSON);

answered Dec 15, 2014 at 7:54

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.