java 返回object映射,JPA将JSON列映射到Java Object

在将大型表迁移到MySQL集群后遇到创建表错误:ERROR1118(42000):行大小过大。考虑将一些列合并为一个JSON对象存储,或者拆分表。通过使用JPA转换器,可以将Java对象序列化为JSON字符串存储在数据库中,从而减少列数并创建表。
摘要由CSDN通过智能技术生成

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): 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;

}

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.

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.

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

解决方案

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 {

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值