我正在尝试使用嵌入式数据库运行集成测试。我试图用hsqldb数据库做这个,利用脚本的使用,当我尝试输入我没有id时,虽然我将我的id设置为自动增量,但我想出了一个例外情况并不是那样我没有#&# 39;包括我的身份证明。
我使用HSQLDB管理器尝试过相同的脚本,一切都适合我,所以我不确定为什么会这样。花了很长时间试图找到解决方案,但我想也许有人可能已经经历过这个。
这是一个java类
package com.whot.dao;
import com.whot.domain.Hotspot;
import com.whot.repository.HotspotRespository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.jdbc.Sql;
import org.springframework.test.context.jdbc.SqlGroup;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import static org.hibernate.validator.internal.util.Contracts.assertNotNull;
import static org.junit.Assert.assertEquals;
@RunWith(SpringJUnit4ClassRunner.class)
@DataJpaTest
@ActiveProfiles("test")
@SqlGroup({
@Sql(executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD, scripts ={"classpath:create_tables.sql"}),
@Sql(executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD, scripts = {"classpath:rollback/rollback_tables.sql"})
})
public class HotspotRepositoryTest {
@Autowired
private HotspotRespository hotpsotRepo;
@Autowired
private TestEntityManager em;
@Test
public void TestSaveHotspot(){
//test code goes here
}
}
这是我的剧本
CREATE TABLE IF NOT EXISTS address (
id BIGINT GENERATED BY DEFAULT AS IDENTITY(START WITH 1, INCREMENT BY 1),
street_name VARCHAR(256),
unit INT,
street_number INT,
CONSTRAINT address_pk PRIMARY KEY (ID)
);
INSERT INTO address(id, street_name, unit, street_number) VALUES (1, 'Ossiomo Street', -1, 2);
INSERT INTO address(street_name, unit, street_number) VALUES ('first street', 10, 780);
这里是堆栈跟踪
Caused by: org.hsqldb.HsqlException: integrity constraint violation: NOT NULL check constraint; SYS_CT_10094 table: ADDRESS column: ID
at org.hsqldb.error.Error.error(Unknown Source) ~[hsqldb-2.3.4.jar:2.3.4]
at org.hsqldb.Table.enforceRowConstraints(Unknown Source) ~[hsqldb-2.3.4.jar:2.3.4]
at org.hsqldb.Table.generateAndCheckData(Unknown Source) ~[hsqldb-2.3.4.jar:2.3.4]
at org.hsqldb.Table.insertSingleRow(Unknown Source) ~[hsqldb-2.3.4.jar:2.3.4]
at org.hsqldb.StatementDML.insertSingleRow(Unknown Source) ~[hsqldb-2.3.4.jar:2.3.4]
at org.hsqldb.StatementInsert.getResult(Unknown Source) ~[hsqldb-2.3.4.jar:2.3.4]
at org.hsqldb.StatementDMQL.execute(Unknown Source) ~[hsqldb-2.3.4.jar:2.3.4]
at org.hsqldb.Session.executeCompiledStatement(Unknown Source) ~[hsqldb-2.3.4.jar:2.3.4]
at org.hsqldb.Session.executeDirectStatement(Unknown Source) ~[hsqldb-2.3.4.jar:2.3.4]
at org.hsqldb.Session.execute(Unknown Source) ~[hsqldb-2.3.4.jar:2.3.4]
答案 0 :(得分:0)
将@GeneratedValue(strategy = GenerationType.IDENTITY)
放在实体类的@Id带注释属性之上。