我正在使用Mysql数据库尝试Sping Boot 。目前我正在尝试测试我的项目,我遇到了以下问题:
我想使用schema.sql设置表,如下所示:
CREATE TABLE IF NOT EXISTS `Ausgaben` (
ID TIMESTAMP NOT NULL,
AMOUNT INT DEFAULT NULL,
PLACE VARCHAR(20) DEFAULT NULL,
DATUM DATE DEFAULT NULL,
CATEGORY varchar(20) DEFAULT NULL,
primary key (ID)
);
字符串
我的application.properties:
spring.datasource.url=jdbc:h2:mem:test;MODE=MySQL;
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.username=testuser
spring.datasource.password=passwort
spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
spring.h2.console.enabled=true
spring.jpa.defer-datasource-initialization=true
spring.jpa.hibernate.ddl-auto=update
spring.sql.init.mode=always
型
我的实体看起来像这样:
@Entity
@Table(name = "Ausgaben")
data class AusgabeEntity(
@Id
val id: Timestamp,
val amount: Double,
val place: String,
val datum: LocalDate,
val category: String
)
型
我的测试看起来像这样:
@Test
fun `post method creates an Ausgabe-Entity in Repository`(){
Assertions.assertEquals(0, ausgabeRepository.count())
logger.info(ausgabeRepository.count().toString())
mockMvc.perform(MockMvcRequestBuilders.post("/save?amount=10&place=lidl&category=haushalt"))
.andExpect(MockMvcResultMatchers.status().isOk)
Assertions.assertEquals(1, ausgabeRepository.count())
logger.info(ausgabeRepository.count().toString())
logger.info(ausgabeRepository.findAll().toString())
}
型
测试工作,但在Stacktrace中,它在执行我的schema.sql时说:
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "create table ausgaben (id datetime(6) not null, amount float(53) not null, category varchar(255), datum date, place varchar(255), primary key (id)) engine=InnoDB" via JDBC [Syntax Fehler in SQL Befehl "create table ausgaben (id datetime(6) not null, amount float(53) not null, category varchar(255), datum date, place varchar(255), primary key (id)) engine[*]=InnoDB"; erwartet "identifier"
Syntax error in SQL statement "create table ausgaben (id datetime(6) not null, amount float(53) not null, category varchar(255), datum date, place varchar(255), primary key (id)) engine[*]=InnoDB"; expected "identifier";]
型
所以它看起来像spring将“engine=InnoDB”附加到我的表定义中,然后无法读取它?
有人能解释一下,这里发生了什么?
谢谢你的帮助
当我修改schema.sql以
CREATE TABLE IF NOT EXISTS `Ausgaben` (
ID TIMESTAMP NOT NULL,
AMOUNT INT DEFAULT NULL,
FAILURE VARCHAR(10) NOT NULL,
PLACE VARCHAR(20) DEFAULT NULL,
DATUM DATE DEFAULT NULL,
CATEGORY varchar(20) DEFAULT NULL,
primary key (ID)
);
型
我可以看到,测试失败,
jakarta.servlet.ServletException: Request processing failed: org.springframework.dao.DataIntegrityViolationException: could not execute statement [NULL nicht zulässig für Feld "FAILURE"
NULL not allowed for column "FAILURE"; SQL statement:
insert into ausgaben (amount,category,datum,place,id) values (?,?,?,?,?) [23502-224]] [insert into ausgaben (amount,category,datum,place,id) values (?,?,?,?,?)]; SQL [insert into ausgaben (amount,category,datum,place,id) values (?,?,?,?,?)]; constraint [null]
型
所以看起来,好像它真的构建了表。所以我不明白为什么它会抛出错误。
1条答案
按热度按时间g6ll5ycj1#
对spring.jpa.database-platform使用org.hibernate.dialect.H2Dialect