![](/img/trans.png)
[英]I am trying to use H2 in-memory database and custom @GenericGenerator strategy but internally my customeId generator is not getting called
[英]H2 in memory database is getting confused between column name, and the value I want to use
上下文:嘗試測試Spring Boot應用程序,嘗試通過src / test / resources /目錄中的data.sql文件初始化數據
我在內存db中具有以下內容:
spring.datasource.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;
我也有以下SQL表:
CREATE TABLE `roles` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`name` varchar(60) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `UK_nb4h0p6txrmfc0xbrd1kglp9t` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
這是我的插入語句(在data.sql中)
INSERT INTO roles(name) VALUES ("ROLE_USER");
當我運行彈簧單元測試時,出現以下錯誤:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Initialization of bean failed; nested exception is org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement #2 of URL [file:/Users/me/IdeaProjects/proj/out/test/resources/data.sql]: INSERT INTO roles(name) VALUES ("ROLE_USER"); nested exception is org.h2.jdbc.JdbcSQLSyntaxErrorException: Column "ROLE_USER" not found; SQL statement:
INSERT INTO roles(name) VALUES ("ROLE_USER") [42122-198]
對我來說,一切看起來都是正確的……我在一個肯定存在的表上使用了一個簡單的插入語句……並且我要插入一個與列定義非常吻合的值。 但是由於某種原因,它拋出該錯誤,指出“ ROLE_USER”列不存在,我不確定為什么。
啊! 我找到了解決方案-事實證明SQL語句的值需要單引號...所以正確的方法是:
INSERT INTO roles(name) VALUES ('ROLE_USER');
代替 :
INSERT INTO roles(name) VALUES ("ROLE_USER");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.