使用 jdbcTemplate 在嵌入式 hsqldb 中测试插入行时出错

Error while testing Insert row in embedded hsqldb with jdbcTemplate

我正在测试我的应用程序的创建方法。 在测试中,我嵌入了 hsql db,并使用 Spring jdbcTemplate

<jdbc:embedded-database id="dataSource" type="HSQL">
    <jdbc:script location="classpath:/sql/db/db-schema.sql"/>
    <jdbc:script location="classpath:/sql/db/db-data.sql"/>
</jdbc:embedded-database>

其中 db-data.sql 是

INSERT INTO C_ContactType(name) VALUES ('bob_eng991');
INSERT INTO C_ContactType(name) VALUES ('bob_eng2');
INSERT INTO C_ContactType(name) VALUES ('bob_eng3');
INSERT INTO C_ContactType(name) VALUES ('bob_eng422');

db-schema.sql 是

  CREATE TABLE C_ContactType (
  contactTypeId INT PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
  name VARCHAR(90) NOT NULL,
  deletedOn DATETIME,
  deletedBy INT
  );

我的测试是

@Test
void test_createClassifier() {

    mockMvc.perform(MockMvcRequestBuilders.post("/classifiers/{category}",  "ContactType")
            .contentType(MediaType.APPLICATION_JSON)
            .param("languageId", String.valueOf(3))
            .content("{ \"name\": \"bob_eng100\"}".getBytes()))
            .andExpect(status().isOk())

}

运行 测试我这里出错

ResultSet rs = jdbcTemplate.insertWithKey(tablePath,
            insert -> insert.columns(paths.toArray(new Path[paths.size()]))
                    .values(params.toArray(new Object[paths.size()]))
                    .executeWithKeys());

其中 tablePath 指向 "C_ContactType" , paths 是一个 ArrayList,其中包含我的 table 列,autoIncrement 字段除外, params 是一个带有值的 ArrayList(autoIncrement 字段除外)

和日志

Caused by: org.hsqldb.HsqlException: user lacks privilege or object not found: contactTypeId
at org.hsqldb.error.Error.error(Unknown Source)
at org.hsqldb.error.Error.error(Unknown Source)
at org.hsqldb.Table.getColumnIndex(Unknown Source)
at org.hsqldb.Table.getColumnIndexes(Unknown Source)
at org.hsqldb.StatementDML.setGeneratedColumnInfo(Unknown Source)
at org.hsqldb.StatementManager.compile(Unknown Source)
at org.hsqldb.Session.execute(Unknown Source)
... 82 more

错误显示框架请求的列的名称。名字大小写混合 contactTypeId.

您的 table 声明在 table 和列名称周围没有双引号字符,因此数据库引擎将名称转换为大写。您需要更改 table 定义:

CREATE TABLE C_ContactType (
"contactTypeId" INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,