当前位置: 首页 > 知识库问答 >
问题:

HSQLException:未找到类型或用户缺乏权限:DATETIMEOFFSET

东郭存
2023-03-14

我有一个Java8、Spring Boot2应用程序,其中JPA实体连接到MS SQL Server数据库。我试图使用HSQLDB(2.3.3)创建集成测试,如果不包括审计信息(创建日期、上次更新等),它可以正常工作。

Caused by: org.hsqldb.HsqlException: type not found or user lacks privilege: DATETIMEOFFSET

我知道“类型找不到或用户缺乏特权”错误是一般性的,可能是由各种原因造成的。在本例中,我知道如果不尝试将datetimeoffset列添加到表中,所有内容都将正确运行。

在联机寻找答案时,我发现了至少支持某些MS SQL日期/时间类型和函数的文档,但这个(两年前的)答案表明HSQLDB对MS SQL的支持有限。我没有找到任何文档专门讨论HSQLDB与datetimeoffset数据类型的关系。

我的表创建和数据插入脚本:

    DROP SCHEMA TEST IF EXISTS;
    CREATE SCHEMA TEST;

    CREATE TABLE TEST.Example (
      ID int IDENTITY NOT NULL,
      Name nvarchar(30) NOT NULL,
      Description nvarchar(1000),
      CreatedDate datetimeoffset(7) NOT NULL
    );

    INSERT INTO TEST.Example (ID, Name, Description, CreatedDate) 
    VALUES (1, 'First', 'This is the first example.', '2019-04-18 12:00:00 -05:00');
    config:
      datasource:
        jdbc-url: jdbc:hsqldb:mem:testdb;sys.syntax_mss=true
        driver-class-name: org.hsqldb.jdbc.JDBCDriver
        validation-query: SELECT 1
        test-on-borrow: false
        test-while-idle: true
        time-between-eviction-runs-millis: 60000
        max-active: 10
    @Data
    @Entity
    @Table(name = "Example", schema = "TEST")
    public class ExampleEntity {    
        @Id
        @Column(name = "ID", unique = true, nullable = false)
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private int id;

        @Column(name = "Name", nullable = false)
        private String name;

        @Column(name = "Description")
        private String description;

        @Column(name = "CreatedDate", nullable = false)
        private LocalDateTime createdDate;    
    }
    public interface ExampleRepository extends CrudRepository<ExampleEntity, Integer> {
    }
    @Configuration
    @EnableJpaRepositories(
        basePackages = "com.test.example.repository",
        entityManagerFactoryRef = "integrationTestEntityFactory",
        transactionManagerRef = "integrationTestTransactionManager")
    public class IntegrationTestConfig {
        @Bean
        @ConfigurationProperties(prefix = "config.datasource")
        public DataSource integrationTestDatasource() {
            return new EmbeddedDatabaseBuilder()
                .addScript("scripts/schema.sql")
                .build();
        }

        @Bean
        public LocalContainerEntityManagerFactoryBean integrationTestEntityFactory(final DataSource integrationTestDatasource) {

            HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
            vendorAdapter.setDatabase(Database.SQL_SERVER);
            vendorAdapter.setShowSql(false);

            LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
            factory.setJpaVendorAdapter(vendorAdapter);
            factory.setPackagesToScan("com.test.example.entity");
            factory.setDataSource(integrationTestDatasource);
            return factory;
        }

        @Bean
        public PlatformTransactionManager integrationTestTransactionManager(final LocalContainerEntityManagerFactoryBean integrationTestEntityFactory) {
            JpaTransactionManager transactionManager = new JpaTransactionManager();
            transactionManager.setEntityManagerFactory(integrationTestEntityFactory.getObject());
            return transactionManager;
        }

        @Bean
        public NamedParameterJdbcTemplate integrationTestJdbcTemplate(final DataSource integrationTestDatasource) {
            return new NamedParameterJdbcTemplate(integrationTestDatasource);
        }
    }

共有1个答案

卢光誉
2023-03-14

对于DateTimeOffsetSQL类型,应该使用Java.time.offsetDateTimeJava类型:

    @Column(name = "CreatedDate", nullable = false)
    private OffsetDateTime createdDate; 

如果您的类路径中有2.2版本中的JPA,就会出现这种情况。否则,您将需要创建一个自定义转换器

 类似资料: