加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Persist PostgreSQL Json datatype with Hibernate

发布时间:2020-12-13 16:52:56 所属栏目:百科 来源:网络整理
导读:在PG数据库中要额外创建以下Function和Cast(必须创建): CREATE OR REPLACE FUNCTION json_intext(text) RETURNS json AS $$SELECT json_in($1::cstring); $$ LANGUAGE SQL IMMUTABLE;CREATE CAST (varchar AS json) WITH FUNCTION json_intext(text) AS I

在PG数据库中要额外创建以下Function和Cast(必须创建):

CREATE OR REPLACE FUNCTION json_intext(text) RETURNS json AS $$
SELECT json_in($1::cstring); 
$$ LANGUAGE SQL IMMUTABLE;

CREATE CAST (varchar AS json) WITH FUNCTION json_intext(text) AS IMPLICIT;

在数据库中表的sql定义

drop table if exists tabx;
create table tabx
(
	id bigint PRIMARY KEY NOT NULL,device_id bigint references hdata_device_info(id),pcie_info json,sample_time timestamp
);

在entity中的类型定义(对应字段使用String类型)

@Entity
@Table(name="hdata_pcie_info")
@NamedQuery(name="HdataPcieInfo.findAll",query="SELECT h FROM HdataPcieInfo h")
public class Tabx implements Serializable
{
	private static final long serialVersionUID = 1L;
	
	@TableGenerator(name="tabx_id_generator",table="id_generator",pkColumnName="pk_name",pkColumnValue="pcie_info_id",valueColumnName="pk_value",allocationSize=1)
    
        @Id
        @GeneratedValue(strategy=GenerationType.TABLE,generator="tabx_id_generator")
	private Long id;

	@Column(name="pcie_info")
	private String pcieInfo;

	@Column(name="sample_time")
	private Timestamp sampleTime;

	//bi-directional many-to-one association to HdataDeviceInfo
	@ManyToOne
	@JoinColumn(name="device_id")
	private HdataDeviceInfo hdataDeviceInfo;
    
        //get or set method

保存json数据:

logger.debug(jsonArray.toString());
entity.setPcieInfo(jsonArray.toString());			
//String json = "{"username":"john","posts":121,"emailaddress":"john@nowhere.com"}";
//entity.setPcieInfo(json)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读