我们有一张很大的桌子,有很多列.移动到MySQL集群后,由于以下原因无法创建表:

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

As an example:

@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对象.

For example:

@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;
}

Where we have defined:

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字符串.

推荐答案

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)

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

@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;
    }
  }

}

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

Json相关问答推荐

在Oracle中循环整个json_opp_t以更改特定键的值

Azure Devops Pipeline:SON字符串变量丢失所有双引号

JSON字符串处理注入引号

如何使用表键名称GROUP_BY

kotlinx-serialization:如何将具有不同类型对象的JsonArray转换为同一个Class

Vega-Lite(Deneb):难以将最小和最大值应用于折线图和文本标签以及线条末尾的点

数据到jsonObject到数据到 struct 是可能的吗?

Python 将 struct 化文本转换和筛选为对象

Android 如何判断小时时间是否在两个时间之间?

如何将一个对象添加到多个对象中 JOLT

将 JSON 解组为具有唯一元素的 map 切片

如何在 Postman 中匹配 json 响应中的内容?并可视化

使用 jq Select 键:值并输出为数组

Google GCM 服务器返回未经授权的错误 401

从 JSON 对象中删除键值对

Jackson 的@JsonView、@JsonFilter 和 Spring

如何使用 Jackson 定义可选的 json 字段

数据包含连续问号时无法理解的 jQuery $.ajax() 行为

Gson 将一组数据对象转换为 json - Android

ASP.NET Web API JSON 输出中没有时间的日期