PostgreSQL-如何在jsonb列中保留数据类型

问题描述

我有一个具有各种微服务的应用程序,一个是针对通用项的。该微服务的表的最后一列是我要存储各种类型的项目的详细信息,因此它是jsonb列。我的问题是,当我尝试填充此列时,我可以编写任何类型的字段。有一种方法可以以持久的方式在jsonb列中设置数据类型?我还希望此数据类型链接到特定项目,并且在另一种项目的情况下,以前的字段设置为null。我已经尝试使用@JsonbProperty做到这一点,但是什么也没发生,我附上我的代码来说明问题所在。

这是通用类

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@Table(name = "ARTICOLI")
@TypeDefs({ @TypeDef(name = "jsonb",typeClass = JsonBinaryType.class) })
public class Articoli<E> implements Serializable {
    private static final long serialVersionUID = 291353626011036772L;

    /**
     * Represents the unique ID for an article object
     */
    @Id
    @Column(name = "ID")
    @GeneratedValue(strategy = GenerationType.SEQUENCE,generator = "my_seq")
    @SequenceGenerator(name = "my_seq",sequenceName = "hibernate_sequence",allocationSize = 1)
    @ApiModelProperty(required = true,notes = "Chiave univoca dell'articolo")
    private Integer id;

    /**
     * Describes an article object.
     */
    @Column(name = "DESCRIZIONE")
    @ApiModelProperty(notes = "Descrizione dell'articolo")
    private String descrizione;

    /**
     * Represents the price for an article object
     */
    @Transient
    @ApiModelProperty(notes = "Prezzo dell'articolo")
    private Double prezzo;


    /**
     * Represents the quantity of available items.
     */
    @Column
    private Integer quantità;
    
    /**
     * Represents the attached file for an article object.
     */
    @Column
    private String pathAllegato;

    @Column
    private String categoria;

    /**
     * Represent the details of specific article
     */
    @Type(type = "jsonb")
    @Column(name = "dettagli",columnDeFinition = "jsonb")
    private E dettagli;

这是我要用其数据类型完全填充dettagli列的特定项目

public class Libro extends Articoli<Libro> {

    /**
     * Unique serialVersionUID of the class Libro
     */
    private static final long serialVersionUID = 8717908017460090411L;

    /**
     * Represents the name of an object of type Libro
     */
    @Type(type = "jsonb")
    @Column(name = "TITOLO",columnDeFinition = "jsonb")
    //@JsonbProperty("titolo")
    @ApiModelProperty(notes = "Titolo del libro")
    private String titolo;

    /**
     * Represents the name of the editorial of an object of type Libro
     */
    @Type(type = "jsonb")
    @Column(name = "REDAZIONE",columnDeFinition = "jsonb")
    //@JsonbProperty("redazione")
    @ApiModelProperty(notes = "Redazione del libro")
    private String redazione;

    @Type(type = "jsonb")
    @Column(name = "AUTORE",columnDeFinition = "jsonb")
    //@JsonbProperty("autore")
    @ApiModelProperty(notes = "Autore del Libro")
    private String autore;

    @Type(type = "jsonb")
    @Column(name = "CASA_EDITRICE",columnDeFinition = "jsonb")
    //@JsonbProperty("casa_editrice")
    @ApiModelProperty(notes = "Casa Editrice del Libro")
    private String caSAEditrice;

    @Type(type = "jsonb")
    @Column(name = "GENERE",columnDeFinition = "jsonb")
    //@JsonbProperty("genere")
    @ApiModelProperty(notes = "Genere del Libro")
    private String genere;

    @Type(type = "jsonb")
    @Column(name = "LINGUA",columnDeFinition = "jsonb")
    //@JsonbProperty("lingua")
    @ApiModelProperty(notes = "Lingua del Libro")
    private String lingua;

    @Type(type = "jsonb")
    @Column(name = "DIMENSIONE",columnDeFinition = "jsonb")
    //@JsonbProperty("dimensione file")
    @ApiModelProperty(notes = "Dimensione del Libro")
    private String dimensioneFile;

    @Type(type = "jsonb")
    @Column(name = "ISBN",columnDeFinition = "jsonb")
    //@JsonbProperty("isbn")
    @ApiModelProperty(notes = "ISBN del Libro")
    private Integer ISBN;

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)