JPA将JSON列映射到Java Object [英] JPA map JSON column to Java Object

查看:2782
本文介绍了JPA将JSON列映射到Java Object的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个包含大量列的大表。在我们转移到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:


ERROR 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)

然后以类似的方式创建类(这会转换一个通用的Object,你可能想要专门化它):

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 Object的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆