如何使用JPA将映射JSON列映射到Java对象

编程入门 行业动态 更新时间:2024-10-28 09:23:22
本文介绍了如何使用JPA将映射JSON列映射到Java对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我们有一个很大的表,有很多列.移至MySQL Cluster后,由于以下原因而无法创建表:

We have a big table with a lot of columns. After we moved to MySQL Cluster, the table cannot be created because of:

错误1118(42000):行大小太大.不包括BLOB在内的已使用表类型的最大行大小为14000.这包括存储开销,请查阅手册.您必须将某些列更改为TEXT或BLOB

ERROR 1118 (42000): Row size too large. The maximum row size for the used table type, not counting BLOBs, is 14000. This includes storage overhead, check the manual. You have to change some columns to TEXT or BLOBs

例如:

@Entity @Table (name = "appconfigs", schema = "myproject") public class AppConfig implements Serializable { @Id @Column (name = "id", nullable = false) @GeneratedValue (strategy = GenerationType.IDENTITY) private int id; @OneToOne @JoinColumn (name = "app_id") private App app; @Column(name = "param_a") private ParamA parama; @Column(name = "param_b") private ParamB paramb; }

这是一个用于存储配置参数的表.我当时想我们可以将一些列合并为一个列,并将其存储为JSON对象,然后将其转换为Java对象.

It's a table for storing configuration parameters. I was thinking that we can combine some columns into one and store it as JSON object and convert it to some Java object.

例如:

@Entity @Table (name = "appconfigs", schema = "myproject") public class AppConfig implements Serializable { @Id @Column (name = "id", nullable = false) @GeneratedValue (strategy = GenerationType.IDENTITY) private int id; @OneToOne @JoinColumn (name = "app_id") private App app; @Column(name = "params") //How to specify that this should be mapped to JSON object? private Params params; }

我们定义的地方:

public class Params implements Serializable { private ParamA parama; private ParamB paramb; }

使用此方法,我们可以将所有列合并为一个并创建表.或者我们可以将整个表拆分为几个表.我个人更喜欢第一个解决方案.

By using this we can combine all columns into one and create our table. Or we can split the whole table into several tables. Personally I prefer the first solution.

无论如何,我的问题是如何映射Params列,该列是文本并包含Java对象的JSON字符串?

Anyway my question is how to map the Params column which is text and contains JSON string of a Java object?

推荐答案

您可以使用JPA转换器将您的实体映射到数据库. 只需在您的params字段中添加与此注释类似的注释:

You can use a JPA converter to map your Entity to the database. Just add an annotation similar to this one to your params field:

@Convert(converter = JpaConverterJson.class)

,然后以类似的方式创建类(这将转换泛型对象,您可能希望对其进行专门化处理):

and then create the class in a similar way (this converts a generic Object, you may want to specialize it):

@Converter(autoApply = true) public class JpaConverterJson implements AttributeConverter<Object, String> { private final static ObjectMapper objectMapper = new ObjectMapper(); @Override public String convertToDatabaseColumn(Object meta) { try { return objectMapper.writeValueAsString(meta); } catch (JsonProcessingException ex) { return null; // or throw an error } } @Override public Object convertToEntityAttribute(String dbData) { try { return objectMapper.readValue(dbData, Object.class); } catch (IOException ex) { // logger.error("Unexpected IOEx decoding json from database: " + dbData); return null; } } }

就是这样:您可以使用此类将表中的任何对象序列化为json.

That's it: you can use this class to serialize any object to json in the table.

更多推荐

如何使用JPA将映射JSON列映射到Java对象

本文发布于:2023-08-01 09:10:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1267138.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   对象   JSON   JPA   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!