首页 > 解决方案 > 在 Java 中使用 Hibernate Criteria 查询从 jsonb postgreSQL 中选择键

问题描述

我正在编写条件查询以在 postgres 中获取 jsonb 的结果。我可以通过 hibernate 查询 jsonb 列。我无法获取 jsonb 特定键。我找不到方法。

我已经尝试过本机查询,但我想在休眠条件查询中找到一些出路。commonjson 参数是 postgresql 中的 jsonb 是这样的——

{"appl_id": 726516, "applied_by": "pankajkumarnnl94@gmail.com", "service_id": 9880004, "version_no": 4, "appl_ref_no": "BSEH/2018/00728", "sub_version": 1, "payment_date": "2018-12-04T11:54:20.24+05:30", "payment_mode": "PayUbizz", "reference_no": "7726929249", "service_name": "Migration Certificate Board of School Education Haryana, Bhiwani", "department_id": 784, "location_value": 1218758, "base_service_id": 988, "department_name": " Board of School Education Haryana", "registration_id": "", "submission_date": "2018-12-04", "submission_mode": "online", "no_of_attachment": 2, "submission_location": "1578947~1218758~Board of School Education Haryana"} 

模型类->ApplInfoJson

package com.saral.reporting.model;

import java.util.Map;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import org.hibernate.annotations.Type;

import com.fasterxml.jackson.annotation.JsonProperty;

@Entity
@Table(name = "r_app_json",schema="saral1", catalog="saral1")

public class ApplInfoJson {

    @Id
    @Column(name = "aid")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long aid;

    @Column(name = "id")
    private Long id;

    @JsonProperty("appl_info")
    @Column(name = "appl_info")
    private String applInfo;


    @Column(name = "application_form_attributes")
    private String applicationFormAttributes;

    @Column(name = "enclosure_data")
    private String enclosureData;

    @Column(name = "service_id")
    private Long serviceId;

    @Column(name = "combined_json")
    @Type(type = "JsonDataUserType")
    private Map<String , Object> combinedJson;

    @Column(name ="location_value")
    private Long locationValue;


    public Long getLocationValue() {
        return locationValue;
    }

    public void setLocationValue(Long locationValue) {
        this.locationValue = locationValue;
    }

    public Long getServiceId() {
        return serviceId;
    }

    public void setServiceId(Long serviceId) {
        this.serviceId = serviceId;
    }

    public Long getAid() {
        return aid;
    }

    public void setAid(Long aid) {
        this.aid = aid;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getApplInfo() {
        return applInfo;
    }

    public void setApplInfo(String applInfo) {
        this.applInfo = applInfo;
    }

    public String getApplicationFormAttributes() {
        return applicationFormAttributes;
    }

    public void setApplicationFormAttributes(String applicationFormAttributes) {
        this.applicationFormAttributes = applicationFormAttributes;
    }

    public String getEnclosureData() {
        return enclosureData;
    }

    public void setEnclosureData(String enclosureData) {
        this.enclosureData = enclosureData;
    }




    public Map<String, Object> getCombinedJson() {
        return combinedJson;
    }

    public void setCombinedJson(Map<String, Object> combinedJson) {
        this.combinedJson = combinedJson;
    }

    @Override
    public String toString() {
        return "ApplInfoJson [aid=" + aid + ", id=" + id + ", applInfo=" + applInfo + ", applicationFormAttributes="
                + applicationFormAttributes + ", enclosureData=" + enclosureData + ", serviceId=" + serviceId
                + ", combinedJson=" + combinedJson + ", locationValue=" + locationValue + "]";
    }



}

使用的功能->

public List<ApplInfoJson> findByCombinedJson(String commonJson) {

        DetachedCriteria criteria = DetachedCriteria.forClass(ApplInfoJson.class);

        List<ApplInfoJson> results = (List<ApplInfoJson>) getHibernateTemplate().findByCriteria(criteria);

        return results;

    }

我想要一些东西来传递键名并使用特定键获取数据。

标签: javajsonpostgresqlhibernatehibernate-criteria

解决方案


推荐阅读