Hibernate 7 and 6
Use the @Type annotation directly on the entity property. You can map to Map, List, POJO, String, or JsonNode.
@Type(JsonType.class)
private Map<String, String> properties = new HashMap<>();
Hibernate 5
Option 1: Provide the fully-qualified name in @Type:
@Type(type = "io.hypersistence.utils.hibernate.type.json.JsonType")
private Map<String, String> properties = new HashMap<>();
Option 2: Define a @TypeDef in your package-info.java to use a shorthand name:
@TypeDef(
name = "json", typeClass = JsonType.class
)
package io.hypersistence.optimizer;
import io.hypersistence.utils.hibernate.type.json.JsonType;
import org.hibernate.annotations.TypeDef;
Then use the shorthand in your entity:
@Type(type = "json")
private Map<String, String> properties = new HashMap<>();
Best Practice
When mapping JSON to a POJO, List<POJO>, or Map<String, POJO>, ensure the POJO overrides equals and hashCode based on its JSON content to prevent unnecessary Hibernate dirty checking updates.