HsqlException:找不到类型或用户缺少权限:DATETIMEOFFSET
HsqlException: type not found or user lacks privilege: DATETIMEOFFSET
我有一个 Java 8,Spring Boot 2 应用程序,其中 JPA 实体连接到 MS SQL 服务器数据库。我正在尝试使用 HSQLDB (2.3.3) 创建集成测试,如果我不包含审计信息(创建日期、上次更新等),它可以正常工作。
这是我收到的错误消息:
Caused by: org.hsqldb.HsqlException: type not found or user lacks privilege: DATETIMEOFFSET
我知道 'type not found or user lacks privilege' 错误是普遍性的,可能由多种原因引起。在这种情况下,我知道如果不尝试将 datetimeoffset 列添加到 table.
,一切都会 运行 正确
在网上寻找答案时,我发现 this documentation that at least some MS SQL date/time types and functions are supported, but 表明 HSQLDB 对 MS SQL 的支持有限。我还没有找到任何专门讨论 HSQLDB 与 datetimeoffset 数据类型相关的文档。
我的 Table 创建和数据插入脚本:
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
最新版本的HSQLDB支持MSS兼容模式下的DATETIMEOFFSET。使用 2.4.1.
如果您想继续使用不支持此类型的旧版本,请在创建表之前使用 CREATE TYPE DATETIMEOFFSET AS TIMESTAMP WITH TIME ZONE
。
我有一个 Java 8,Spring Boot 2 应用程序,其中 JPA 实体连接到 MS SQL 服务器数据库。我正在尝试使用 HSQLDB (2.3.3) 创建集成测试,如果我不包含审计信息(创建日期、上次更新等),它可以正常工作。
这是我收到的错误消息:
Caused by: org.hsqldb.HsqlException: type not found or user lacks privilege: DATETIMEOFFSET
我知道 'type not found or user lacks privilege' 错误是普遍性的,可能由多种原因引起。在这种情况下,我知道如果不尝试将 datetimeoffset 列添加到 table.
,一切都会 运行 正确在网上寻找答案时,我发现 this documentation that at least some MS SQL date/time types and functions are supported, but
我的 Table 创建和数据插入脚本:
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
最新版本的HSQLDB支持MSS兼容模式下的DATETIMEOFFSET。使用 2.4.1.
如果您想继续使用不支持此类型的旧版本,请在创建表之前使用 CREATE TYPE DATETIMEOFFSET AS TIMESTAMP WITH TIME ZONE
。