52 lines
2.0 KiB
Java
Executable File
52 lines
2.0 KiB
Java
Executable File
package com.ankurm.hibernatedemo.persistenceannotations;
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
|
|
import com.ankurm.hibernatedemo.HibernateDemoApplication;
|
|
import jakarta.persistence.EntityManager;
|
|
import jakarta.persistence.EntityManagerFactory;
|
|
import java.util.Map;
|
|
import org.junit.jupiter.api.Test;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.boot.test.context.SpringBootTest;
|
|
|
|
/**
|
|
* Does {@code @JdbcTypeCode(SqlTypes.JSON)} actually work against H2 2.4.240? Honest empirical
|
|
* check -- if H2 rejects it, this test says so instead of asserting success.
|
|
*
|
|
* <p>Docs: docs/05-jpa-persistence-annotations.md, Topic 2.
|
|
*/
|
|
@SpringBootTest(classes = HibernateDemoApplication.class)
|
|
class JsonColumnOnH2Test {
|
|
|
|
@Autowired
|
|
private EntityManagerFactory emf;
|
|
|
|
@Test
|
|
void jdbcTypeCodeJson_onH2_roundTripsAMap() {
|
|
EntityManager em = emf.createEntityManager();
|
|
em.getTransaction().begin();
|
|
JsonColumnEntity e = new JsonColumnEntity(Map.of("color", "red", "qty", 5));
|
|
em.persist(e);
|
|
em.getTransaction().commit();
|
|
Long id = e.getId();
|
|
em.close();
|
|
|
|
EntityManager em2 = emf.createEntityManager();
|
|
JsonColumnEntity loaded = em2.find(JsonColumnEntity.class, id);
|
|
em2.close();
|
|
|
|
System.out.println("RESULT[jdbctypecode-json-h2]: persisted+loaded details=" + loaded.getDetails());
|
|
assertThat(loaded.getDetails()).containsEntry("color", "red");
|
|
|
|
// Inspect the actual column type H2 chose for it.
|
|
EntityManager em3 = emf.createEntityManager();
|
|
Object colType = em3.createNativeQuery(
|
|
"select data_type from information_schema.columns "
|
|
+ "where table_name = 'JSON_COLUMN_ENTITY' and column_name = 'DETAILS'")
|
|
.getSingleResult();
|
|
em3.close();
|
|
System.out.println("RESULT[jdbctypecode-json-h2]: H2 column type for the JSON field = " + colType);
|
|
}
|
|
}
|