Switch from JsonStringType to JsonBinaryType when the project uses both MySQL and PostgreSQL(项目同时使用 MySQL 和 PostgreSQL 时从 JsonStringType 切换到 JsonBinaryType)
问题描述
当需要从 PostgreSQL 切换到 MariaDB/MySql 时,我的 json 列有问题.
我使用 Spring Boot + JPA + Hibernate + hibernate-types-52.
我要映射的表是这样的:
I have a problem with column json when it's necessary to switching from PostgreSQL to MariaDB/MySql.
I use Spring Boot + JPA + Hibernate + hibernate-types-52.
The table i want to map is like this:
CREATE TABLE atable(
 ...
 acolumn JSON,
 ... 
);
好的,它适用于 PostgreSQL 和 MariaDB/MySql.
问题是当我想部署一个应用程序时,因为 PostgreSQL 和 MySQL/MariaDB 的正确 hibernate-types-52 实现不同,所以可以轻松地从一个切换到另一个应用程序
这适用于 MySQL/MariaDB
Ok it works for PostgreSQL and MariaDB/MySql.
The problem is when i want to deploy an application that switch easly from one to another because the correct hibernate-types-52 implementation for PostgreSQL and MySQL/MariaDB are different
This works on MySQL/MariaDB
@Entity
@Table(name = "atable")
@TypeDef(name = "json", typeClass = JsonStringType.class)
  public class Atable {
  ...
  @Type(type = "json")
  @Column(name = "acolumn", columnDefinition = "json")
  private JsonNode acolumn;
  ...
}
这适用于 PosgreSQL
This works on PosgreSQL
@Entity
@Table(name = "atable")
@TypeDef(name = "json", typeClass = JsonBinaryType.class)
public class Atable {
  ...
  @Type(type = "json")
  @Column(name = "acolumn", columnDefinition = "json")
  private JsonNode acolumn;
  ...
}
任何从 JsonBinaryType 切换到 JsonStringType 的解决方案(或解决此问题的任何其他解决方案)都值得赞赏.
Any kind of solutions to switch from JsonBinaryType to JsonStringType (or any other solution to solve this) is appreciated.
推荐答案
从 2.11 版本的 Hibernate Types 项目开始,您可以只使用 JsonType,它适用于 PostgreSQL、MySQL、Oracle、SQL Server 或 H2.
Starting with the 2.11 version of the Hibernate Types project, you can just use the JsonType, which works with PostgreSQL, MySQL, Oracle, SQL Server, or H2.
所以,使用 JsonType 而不是 JsonBinaryType 或 JsonStringType
So, use JsonType instead of JsonBinaryType or JsonStringType
@Entity
@Table(name = "atable")
@TypeDef(name = "json", typeClass = JsonType.class)
public class Atable {
  @Type(type = "json")
  @Column(name = "acolumn", columnDefinition = "json")
  private JsonNode acolumn;
}
就是这样!
这篇关于项目同时使用 MySQL 和 PostgreSQL 时从 JsonStringType 切换到 JsonBinaryType的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:项目同时使用 MySQL 和 PostgreSQL 时从 JsonStringType 切换到 JsonBinaryType
				
        
 
            
        基础教程推荐
- MySQL 5.7参照时间戳生成日期列 2022-01-01
 - ORA-01830:日期格式图片在转换整个输入字符串之前结束/选择日期查询的总和 2021-01-01
 - 从字符串 TSQL 中获取数字 2021-01-01
 - 使用 VBS 和注册表来确定安装了哪个版本和 32 位 2021-01-01
 - 如何在 CakePHP 3 中实现 INSERT ON DUPLICATE KEY UPDATE aka upsert? 2021-01-01
 - 带更新的 sqlite CTE 2022-01-01
 - while 在触发器内循环以遍历 sql 中表的所有列 2022-01-01
 - CHECKSUM 和 CHECKSUM_AGG:算法是什么? 2021-01-01
 - 带有WHERE子句的LAG()函数 2022-01-01
 - MySQL根据从其他列分组的值,对两列之间的值进行求和 2022-01-01
 
    	
    	
    	
    	
    	
    	
    	
    	
						
						
						
						
						
				
				
				
				