代码之家  ›  专栏  ›  技术社区  ›  Peter Penzov

将@Type(Type=“org.hibernate.Type.ClassType”)迁移到Spring Boot 3

  •  0
  • Peter Penzov  · 技术社区  · 1 年前

    我有以下代码,我想将其迁移到Spring Boot 3:

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

    示例演示:

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

    我得到错误:

    Cannot resolve method 'type'
    

    你知道我如何迁移这些代码吗?

    编辑:

    我尝试使用:

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

    我收到以下错误:

    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)
    
    0 回复  |  直到 1 年前
        1
  •  1
  •   Sheheryar Aamir    1 年前

    Spring Boot 3升级到Hibernate 6。 使用Hibernate 6 @Type 注释可以替换为 @JdbcTypeCode 以使代码正常工作。

    @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
    }
    }