'How to persist List<Object> as jsonb in hibernate

Below the Entity trying to persist :

@Data
@Entity
@Table(name = "REQUEST")
@TypeDef(name = "jsonb", typeClass = JsonBinaryType.class)
public class Request {

@Id
@Column(name = "ID")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;

@Column(name = "CODE", nullable = false)
private String code;

@NotNull
@Column(name = "CREATED_BY", nullable = false)
private String createdBy;

@Type(type = "jsonb")
@Column(name = "REQUEST_PAYLOAD", columnDefinition = "jsonb", nullable = false)
private List<RequestPayload> requestPayload;

@Column(name = "CREATED_DATE")
private LocalDateTime createdDate;

@Override
public Request createRequest(Request request) {
    LOG.info("Request to save request: {}", request);
    return requestRepository.save(request);
}

While persisting it's throwing exception as

could not serialize; nested exception is org.hibernate.type.SerializationException: could not serialize

It's failing beoz of

@Type(type = "jsonb")
@Column(name = "REQUEST_PAYLOAD", columnDefinition = "jsonb", nullable = false)
private List<RequestPayload> requestPayload;

if i change List to JsonNode then it works fine



Solution 1:[1]

Use

public class Request implements Serializable {
...
}

This issue is because the library cannot serialize the POJO to JSON

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 prabhup