I am new to coding and I am currently working with PostgresSQL and Spring-boot. I want to store an Object and one field is an List of doubles. I know, that I have to save it as JSON-Object (JSONB). I really have no idea what the problem is. Maybe someone have an idea or hints. Thanks in advance :)
I get the following error:
2025-03-06 13:44:59 WARN o.h.e.jdbc.spi.SqlExceptionHelper - SQL Error: 0, SQLState: 42804 2025-03-06 13:44:59 ERROR o.h.e.jdbc.spi.SqlExceptionHelper - ERROR: column "embedding" is of type jsonb but expression is of type bigint Hinweis: You will need to rewrite or cast the expression. Position: 159
My EntityClass is this:
package de.mlp.pa.berufesuche.persistence.entity;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.persistence.*;
import .hibernate.annotations.ColumnTransformer;
import .slf4j.Logger;
import .slf4j.LoggerFactory;
import java.util.List;
@Entity
@Table(name = "enrichments")
@SequenceGenerator(name = "enrichments_seq", sequenceName = "enrichments_seq", allocationSize = 1)
public class EnrichmentEntity {
private static final Logger logger = LoggerFactory.getLogger(EnrichmentEntity.class);
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "PROFESSION")
private String profession;
@Embedded
private DocumentEntity document;
@Column(name = "INSURANCE")
private String insurance;
@Lob
@Column(name = "EMBEDDING", columnDefinition = "JSONB")
private String embedding;
public EnrichmentEntity() {}
public EnrichmentEntity(String profession, DocumentEntity document, String insurance, List<Double> embedding) {
this.profession = profession;
this.document = document;
this.insurance = insurance;
this.embedding = convertListToJson(embedding);
}
// Getter und Setter
public List<Double> getEmbeddingAsList() {
return convertJsonToList(this.embedding);
}
public void setEmbedding(List<Double> embedding) {
this.embedding = convertListToJson(embedding);
}
private String convertListToJson(List<Double> list) {
try {
ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writeValueAsString(list);
logger.debug("Converted List<Double> to JSON: {}", json); // Logge die JSON-Zeichenkette
return json;
} catch (JsonProcessingException e) {
logger.error("Fehler beim Konvertieren der Embeddings in JSON", e);
throw new RuntimeException("Fehler beim Konvertieren der Embeddings in JSON", e);
}
}
private List<Double> convertJsonToList(String json) {
try {
ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.readValue(json, new TypeReference<List<Double>>() {});
} catch (JsonProcessingException e) {
logger.error("Fehler beim Konvertieren von JSON zu Embeddings", e);
throw new RuntimeException("Fehler beim Konvertieren von JSON zu Embeddings", e);
}
}
}
In my database i run this sql:
CREATE TABLE enrichments (
id SERIAL PRIMARY KEY,
profession TEXT NOT NULL,
insurance TEXT,
embedding JSONB,
profession_name TEXT,
synonyms TEXT,
job_groups TEXT,
description TEXT,
abbreviations TEXT,
full_written TEXT
);