Search code examples
springspring-boothibernatespring-data-jpanhibernate-mapping

Migrate @Type(type = "org.hibernate.type.ClassType") to Spring Boot 3


I have the following code which I want to migrate to Spring Boot 3:

  @Column(name = "OPTION_TYPE")
  @Type(type = "org.hibernate.type.ClassType")
  private Class<T> clazz;

Example demo:

https://github.com/rcbandit111/jpa_class_store_migration_poc/blob/main/src/main/java/com/jpa/store/poc/EnumeratedAccountOption.java

I get error:

Cannot resolve method 'type'

Do you know how I can migrate this code?

EDIT:

I tried to use:

@Column(name = "OPTION_TYPE")
@JdbcTypeCode(SqlTypes.JAVA_OBJECT)
private Class<T> clazz;

I get this error:

Caused by: jakarta.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Unable to determine SQL type name for column 'option_type' of table 'account_options' because there is no type mapping for org.hibernate.type.SqlTypes code: 2000 (JAVA_OBJECT)
        at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:421) ~[spring-orm-6.1.5.jar!/:6.1.5]
        at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:396) ~[spring-orm-6.1.5.jar!/:6.1.5]
        at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:366) ~[spring-orm-6.1.5.jar!/:6.1.5]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1833) ~[spring-beans-6.1.5.jar!/:6.1.5]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1782) ~[spring-beans-6.1.5.jar!/:6.1.5]
        ... 92 common frames omitted
Caused by: org.hibernate.MappingException: Unable to determine SQL type name for column 'option_type' of table 'account_options' because there is no type mapping for org.hibernate.type.SqlTypes code: 2000 (JAVA_OBJECT)

Solution

  • Spring Boot 3 upgraded to Hibernate 6. with Hibernate 6 @Type annotation can be replaced with @JdbcTypeCode to make the code work.

    @Column(name = "OPTION_TYPE")
    @JdbcTypeCode(SqlTypes.JAVA_OBJECT)
    private Class<T> clazz;
    

    Basic Test class

    @TestInstance(value=Lifecycle.PER_CLASS)
    public class EnumeratedAccountOptionTest {
     private EnumeratedAccountOption<ExampleEnum> option;
    
    @BeforeAll
    public void setUp() {
        option = new EnumeratedAccountOption<>("Test Option", ExampleEnum.VALUE_ONE);
    }
    
    @Test
    public void testGetOptionValue() {
        assertEquals(ExampleEnum.VALUE_ONE, option.getOptionValue());
    }
    
    @Test
    public void testGetClazz() {
        assertEquals(ExampleEnum.class, option.getClazz());
    }
    @Test
    public void testSetClazz() {
        option.setClazz(ExampleEnum.class);
        assertEquals(ExampleEnum.class, option.getClazz());
    }
    private enum ExampleEnum {
        VALUE_ONE,
        VALUE_TWO
    }
    }