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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15836403/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 21:00:08  来源:igfitidea点击:

how to store a json object to a hibernate database field

javajson

提问by thanili

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

我的情况如下。我有一个带有三个输出文本和相应输入文本的 JSF 表单,可以说:

outputtext1 - inputtext1

输出文本 1 - 输入文本 1

outputtext2 - inputtext2

输出文本2 - 输入文本2

outputtext3 - inputtext3

输出文本3 - 输入文本3

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.

这是一个指示性示例,表单将动态生成并即时创建。

采纳答案by Fritz

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.

为什么要序列化/反序列化 JSON 以将其直接发送到数据库?反序列化有它自己的问题,并多次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, FieldRecordentities that contain a key - value pair.

您的实体可以具有一个Map<String,String>属性或一组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. 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.

我建议这样做是因为数据库中的 JSON 可能会导致复杂的问题,特别是如果您稍后必须查询该数据。您必须处理 JSON 才能报告或查找哪些记录具有特定字段。这只是一个例子,事情可能会变得更复杂。

回答by Stephan

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

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

回答by rai.skumar

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

转换JSONObjectString表格然后存储。当你读回来时,将其转换回JSONObject如下所示:

       JSONObject obj = new JSONObject(stringRepresentationOfJSON);

回答by Aditya Singh

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

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