源码地址:https://gitee.com/yk001/base.git
1,引入hibernate-types-5
<dependency>
<groupId>com.vladmihalcea</groupId>
<artifactId>hibernate-types-52</artifactId>
<version>2.9.9</version>
</dependency>
2,表结构
CREATE TABLE "public"."base_entity" (
"id" int4 NOT NULL DEFAULT nextval('base_entity_id_seq'::regclass),
"name" varchar(60) COLLATE "pg_catalog"."default",
"remark" json,
CONSTRAINT "base_entity_pkey" PRIMARY KEY ("id")
)
;
ALTER TABLE "public"."base_entity"
OWNER TO "postgres";
2,Entity Demo
package com.yukang.base.dao.entity;
import com.alibaba.fastjson.JSONObject;
import com.vladmihalcea.hibernate.type.json.JsonBinaryType;
import lombok.Data;
import org.hibernate.annotations.Type;
import org.hibernate.annotations.TypeDef;
import javax.persistence.*;
@Data
@Table(name="base_entity")
@Entity
@TypeDef(name = "remark", typeClass = JsonBinaryType.class)
public class BaseEntity {
@Id
@SequenceGenerator(name="baseEntityIdSeq",sequenceName="base_entity_id_seq",allocationSize=1)
@GeneratedValue(strategy = GenerationType.SEQUENCE,generator="baseEntityIdSeq")
private Long id;
private String name;
@Type(type = "remark")
private JSONObject remark;
}
4,验证
保存:
curl -X POST "http://localhost:9001/baseentity" -H "accept: */*" -H "Content-Type: application/json" -d "{ \"name\": \"test\", \"remark\": { \"k1\": \"v1\" }}"
查询: