简体   繁体   English

如何将json对象存储到休眠数据库字段

[英]how to store a json object to a hibernate database field

my case is the following. 我的情况如下。 I have a JSF form with three outputtexts & the corresponding inputtexts lets say: 我有一个JSF表单,其中包含三个输出文本和相应的输入文本,可以这样说:

outputtext1 - inputtext1 outputtext1-inputtext1

outputtext2 - inputtext2 outputtext2-inputtext2

outputtext3 - inputtext3 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. 目前,我使用backbean方法“保存”以将它们存储到数据库中(休眠对象让主键为table1.id的table1进入字段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"} {“ 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! 我正在考虑将表单数据作为JSON对象进行操作...但是我对Java + JSON还是陌生的,因此一些准则对我真的很有用!

This is an indicative example, form are going to by dynamic and created on the fly. 这是一个指示性示例,表单将动态创建并动态创建。

Why would you want to serialize/deserialize a JSON to send it directly to the database? 为什么要序列化/反序列化JSON以直接将其发送到数据库? Deserialization has its own issues and multiple deserializations could (not will ) be a source of issues. 反序列化有它自己的问题,并多次deserializations 可能 (不是 )是问题的根源。

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. 您应该将字段保存为给定实体的属性,然后借助Gson之类的库从该实体生成JSON。


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. 您的实体可以具有Map<String,String>属性,也FieldRecord是包含键值对的FieldRecord实体的集合。

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. 我建议这样做是因为数据库中的JSON可能导致复杂的问题,尤其是如果您稍后必须查询该数据时。 YOu'll have to process the JSONs in order to either report or find which records have a particular field. 您必须处理JSON才能报告或查找具有特定字段的记录。 This is just an example, things can get more complex. 这只是一个例子,事情可能会变得更加复杂。

很简单,您需要在表中存储BLOB类型的列,以便存储json,而在Java中检索它时,只需解码json即可,我建议使用https://code.google.com/p/json-simple/其很简单的

Convert JSONObject into a String form and then store. JSONObject转换为String形式,然后进行存储。 And when your read it back, convert it back into JSONObject like below : 当您读回它时,将其转换回JSONObject如下所示:

       JSONObject obj = new JSONObject(stringRepresentationOfJSON);

像这样更改休眠映射JSONObject obj = new JSONObject(stringRepresentationOfJSON);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM