简体   繁体   中英

Ignore some fields for JSONB Deserialization in Spring JPA

I have a Spring JPA Model

@Entity
@Table(name = "projects")
@TypeDefs({ @TypeDef(name = "jsonb", typeClass = JsonBinaryType.class) })
public class Project implements Serializable {

  @Id
  private long id;


  @Column(name = "version_id")
  private Long version;

  @Column(name = "created_by")
  private String createdBy;

  @Column(name = "created_at")
  private LocalDateTime createdAt;

  @Column(name = "updated_by")
  private String updatedBy;

  @Column(name = "updated_at")
  private LocalDateTime updatedAt;

  @Column(name = "is_archived")
  private Boolean archived;

  private String commentsRoomId;

  private String workflowType;
  private String workflowId;

  @Type(type = "jsonb")
  @Column(columnDefinition = "jsonb")
  private Map<String, ProjectSection> sections;

Now the data in the last property is a JSONB Property which should ignore some of the fields. as it wouldnt be present in the DB either.

Now when I try to get the data I get the following error.

The given Json object value: {s06=ProjectSection(version=1, enabled=true, type=milestones, values={}, visibility=null, sectionId=null, sectionSchema=null)

which says this after the json repr.

Now my Section looks like

@NoArgsConstructor
@AllArgsConstructor
@Data
public class ProjectSection {

  @JsonProperty("version_id")
  private Long version;

  @JsonProperty("enabled")
  private Boolean enabled;

  @JsonProperty("type")
  private String type;

  @JsonProperty("values")
  private Map<String, ProjectField> values;

  @JsonProperty("visibility")
  private List<String> visibility;

  // Not Srtored in DB
  @JsonIgnore
  @Transient
  private String sectionId;

  @JsonIgnore
  @Transient
  private ProjectTemplate.SectionSchema sectionSchema;

I think maybe because of it being in a JSONB Nested fields the transient doesn't make sense. How should I make sure these values are ignored when its deserialising from the DB and loads properly. If I remove these extra fields, it seems to work.

Why don't you use some mappers ModelMapper or DozerMapper to handle such cases.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM