gpt4 book ai didi

java - Hsql异常: type not found or user lacks privilege: DATETIMEOFFSET

转载 作者:行者123 更新时间:2023-12-02 01:33:02 25 4
gpt4 key购买 nike

我有一个 Java 8、Spring Boot 2 应用程序,其中 JPA 实体连接到 MS SQL Server 数据库。我正在尝试使用 HSQLDB (2.3.3) 创建集成测试,如果我不包含审核信息(创建日期、上次更新等),该测试可以正常工作。

这是我收到的错误消息:

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

据我了解,“未找到类型或用户缺乏权限”错误是通用错误,可能由多种原因引起。在这种情况下,我知道如果不是尝试将 datetimeoffset 列添加到表中,一切都会正确运行。

在网上寻找答案时,我发现this documentation至少支持一些 MS SQL 日期/时间类型和函数,但是 this (two-year-old) answer表明 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);
}
}

最佳答案

对于 datetimeoffset SQL 类型,您应该使用 java.time.OffsetDateTime Java 类型:

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

这是如果您的类路径中有 2.2 版本的 JPA。否则你需要创建一个 custom converter

关于java - Hsql异常: type not found or user lacks privilege: DATETIMEOFFSET,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55751478/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com