当需要从 PostgreSQL 切换到 MariaDB/MySql 时,我遇到了 json 列的问题。
我使用 Spring Boot + JPA + Hibernate + hibernate-types-52。
我想要映射的表是这样的:
CREATE TABLE atable(
...
acolumn JSON,
...
);
好的,它适用于 PostgreSQL 和 MariaDB/MySql。
问题是当我想要部署一个可以轻松从一个应用程序切换到另一个应用程序时,因为 PostgreSQL 和 MySQL/MariaDB 的正确 hibernate-types-52 实现是不同的
这适用于 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;
...
}
这在 PostgreSQL 中有效
@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 的解决方案(或解决此问题的任何其他解决方案)都是值得赞赏的。
The 超坚持实用程序 https://github.com/vladmihalcea/hypersistence-utils项目,你可以使用JsonType
,适用于 PostgreSQL、MySQL、Oracle、SQL Server 或 H2。
So, use JsonType
代替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;
}
就是这样!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)