当前位置:网站首页>jpa + MySQL list转String存入数据库
jpa + MySQL list转String存入数据库
2022-06-10 11:03:00 【andQVQ】
问题
list通过jpa直接存入数据库会报错这里需要进行转换
思考
list中存放的自定义对象为Point等一类位置坐标,可以使用POINT、MULTIPOINT等空间类型的字段
MySQL数据类型list中存放的是基本数据或者自定义基本数据的集合
在POJO类中定义转换器
@Entity
@Data
public class MyClass{
@Convert(converter = JpaConverterListJson.class)
private List<MyList> positions;
}
对于List中为基本类型的数据,使用的转换方式
import com.alibaba.fastjson.JSON;
import javax.persistence.AttributeConverter;
/** * @ClassName JpaConverterListJson * @Description jpa list转换为test 相互转换工具类 * @Author ygt * @Date 2021/3/3 14:49 * @Version V1.0 */
public class JpaConverterListJson implements AttributeConverter<Object, String> {
@Override
public String convertToDatabaseColumn(Object o) {
return JSON.toJSONString(o);
}
@Override
public Object convertToEntityAttribute(String s) {
return JSON.parseArray(s);
}
}
对于List中为基本类型集合的数据,使用的转换方式
import com.alibaba.fastjson.JSON;
import javax.persistence.AttributeConverter;
/** * @ClassName JpaConverterListJson * @Description jpa list转换为test 相互转换工具类 * @Author ygt * @Date 2021/3/3 14:49 * @Version V1.0 */
public class JpaConverterListJson implements AttributeConverter<List<MyList>, String> {
@Override
public String convertToDatabaseColumn(List<MyList> o) {
return JSON.toJSONString(o);
}
@Override
public List<MyList> convertToEntityAttribute(String s) {
return JSON.parseArray(s, MyList.class);
}
}
这样就可以在数据库中存入List转换的Json数据
P.S.
在POJO类中定义转换器时,如果使用注解@ElementCollection,Hibernate在存储数据时会自动为该集合单独创建一张表,这个表包含一个指向该属性所在类ID的外键。
@Entity
@Data
public class MyClass{
@Convert(converter = JpaConverterListJson.class)
@ElementCollection
private List<MyList> positions;
}
边栏推荐
- [C language] pragma pack (1) and pragma pack ()
- 蔚来季报图解:营收99亿同比增24% 经营亏损近22亿
- 保温热水更方便,日常泡奶的小神器,七彩叮当无线便携式调奶器上手
- 数商云商业服务业SaaS管理系统:实现高效业务协作,助力企业完善数字化转型之路
- No subnet partition? Rego will help you understand IP addresses and teach you how to divide subnets!
- Review the growth evaluation of central enterprises and listed companies
- 常用颜色RGB、灰度值、取色值、透明度。
- Jiachuang biology rushes to the Growth Enterprise Market: the actual controller with annual revenue of 130million was once a professor of Wuhan University
- 【BUUCTF】[Zer0pts2020]Can you guess it?
- 在线文档协作工具,是提高工作效率的第一步
猜你喜欢

Fcpx plug-in: premiumvfx animation presets v1.0.1 Special Edition

保温热水更方便,日常泡奶的小神器,七彩叮当无线便携式调奶器上手

Nucleic acid detection robot

核酸检测机器人

Vite's public directory

深度剖析「圈组」关系系统设计 | 「圈组」技术系列文章

Implementation code of several recent good papers (with source code download)

单片机触发器或非门工作原理以及用途

C#大作业——学生信息管理系统

更耐用的遊戲真無線耳機,電池超大續航持久,英雄G1上手
随机推荐
PV operation daily question - orange apple question (advanced version)
NeRF:用深度学习完成3D渲染任务的蹿红
Carbon reduction in the construction industry is by no means a fresh idea experts suggest strengthening the transformation of rural buildings
你的下一台电脑何必是电脑,探索不一样的远程操作
如何才能把团队给带解散。。。
Efficiency tools: utools
Conversion between binary, octal, decimal and hexadecimal (integer plus decimal)
Why is your next computer a computer? Explore different remote operations
Modstartcms enterprise content site building system (supporting laravel9) v4.1.0
Fragment implements the switch of the bottom navigation bar without refreshing
MySQL practice 45 lectures_ 8_ Deepen the understanding of MySQL repeatable reading from a problem
Meetup回顾|DevOps&MLOps如何在企业中解决机器学习困境?
Golang cli framework --cobra
string类及学习使用文档
Pat class a 1134 vertex coverage
关于Visual Studio 2019安装时VS installer无法下载文件,进度条为0,显示网络有问题的解决办法
[communication protocol] UART, I2C, SPI review
【黄啊码】老师,高考后我想选软件开发相关专业,你觉得选哪个方向好?高考志愿怎么填报?
RT thread usage manual (updated on 2022-0516)
Gan learning notes KL divergence, JS divergence, Wasserstein distance