0

I am trying to implement database entity with PanacheEntity. I am using hibernate ORM with panache extension for quarkus (https://quarkus.io/guides/hibernate-orm-panache)

The columns are generated dynamically and therefore I used Map data structure to store keys and values.

This is my implementation:

import io.quarkus.hibernate.orm.panache.PanacheEntity;
import jakarta.persistence.Entity;
import jakarta.persistence.Table;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

@Entity
@Table(name = "table")
public class MyEntity extends PanacheEntity {

  private Map<String, String> fields = new HashMap<>();

  private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

  public <T> T getField(String key, Class<T> valueType) {
    String value = fields.get(key);
    try {
      return OBJECT_MAPPER.readValue(value, valueType);
    } catch (IOException e) {
      throw new RuntimeException("Failed to deserialize value for key: " + key, e);
    }
  }

  public void setField(String key, Object value) {
    try {
      String stringValue = OBJECT_MAPPER.writeValueAsString(value);
      fields.put(key, stringValue);
    } catch (JsonProcessingException e) {
      throw new RuntimeException("Failed to serialize value for key: " + key, e);
    }
  }

  public Map<String, String> getFields() {
    return fields;
  }

  public void setFields(Map<String, String> fields) {
    this.fields = fields;
  }

}

Upon starting application I get this exception:

JdbcTypeRecommendationException: Could not determine recommended JdbcType for Java type 'java.util.Map<java.lang.String, java.lang.String>'

Does someone have some workaround for this issue?

2
  • Removed my prev comment, I think I understand what you're trying to achieve. Hibernate doesn't know how to persist a map like that, that's what the error is. It looks like you want something closer to a document store, i.e. persist an arbitrary structure. Take a read of this: baeldung.com/hibernate-persist-json-object Commented Aug 30, 2024 at 13:48
  • You need to store it as json, see: stackoverflow.com/a/76193688/2979325 Commented Aug 31, 2024 at 22:12

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.