0
我想用以下两个脚本运行HSQL,然后测试它,但每次遇到我无法处理的错误。Spring:HSQL-无法执行数据库脚本
无法执行数据库脚本;嵌套异常是org.springframework.jdbc.datasource.init.ScriptStatementFailedException:无法在资源类路径资源[data.sql]的第4行执行SQL脚本语句:insert into spittle(spitter_id,spittleText,postingTime)values(2,'尝试之春 '新的表达语言', '2010-06-11')
更新 - 其他异常已被抛出:
完整性约束违规:外键无父母; SYS_FK_10108 表:唾沫
这是我的脚本:
schema.sql文件
drop table if exists spittle;
drop table if exists spitter;
create table spitter (
id identity,
username varchar(25) not null,
password varchar(25) not null,
fullname varchar(100) not null,
email varchar(50) not null,
update_by_email boolean not null
);
create table spittle (
id integer identity primary key,
spitter_id integer not null,
spittleText varchar(2000) not null,
postedTime date not null,
foreign key (spitter_id) references spitter(id)
);
data.sql
insert into spitter (username, password, fullname, email, update_by_email) values ('habuma', 'password', 'Craig Walls', '[email protected]', false);
insert into spitter (username, password, fullname, email, update_by_email) values ('artnames', 'password', 'Art Names', '[email protected]', false);
insert into spittle (spitter_id, spittleText, postedTime) values (1, 'Have you read Spring in Action 3? I hear it is awesome!', '2010-06-09');
insert into spittle (spitter_id, spittleText, postedTime) values (2, 'Trying out Spring''s new expression language.', '2010-06-11');
insert into spittle (spitter_id, spittleText, postedTime) values (1, 'Who''s going to SpringOne/2GX this year?', '2010-06-19');
appContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<jdbc:embedded-database id="dataSource" type="H2">
<jdbc:script location="classpath:schema.sql" />
<jdbc:script location="classpath:data.sql" />
</jdbc:embedded-database>
</beans>
单元测试
package com.habuma.spitter.persistence;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
public class DataAccessUnitTestTemplate {
private static EmbeddedDatabase db;
@Before
public void setUp() {
// creates a HSQL in-memory db populated from default scripts classpath:schema.sql and classpath:test-data.sql
// obviously, this is the 'in-code' method, but the xml should work for Spring managed tests.
db = new EmbeddedDatabaseBuilder().addDefaultScripts().build();
}
@Test
public void testDataAccess() {
JdbcSpitterDao jdbc = new JdbcSpitterDao(db);
System.out.println(jdbc.getSpitterById(1L));
}
@After
public void tearDown() {
db.shutdown();
}
}
@SotiriosDelimanolis预处理语句看起来像这样'SQL_SELECT_SPITTER = “从spitter选择ID,用户名,全名其中id =?”'。但是我测试了应用程序,并且它在'setUp()'方法上失败了(另外也有其他评论)。 – ashur
@SotiriosDelimanolis你能更具体吗?我不确定我应该删除什么。我更新了第二个例外的问题。 – ashur
@SotiriosDelimanolis它没有帮助。但是我刚刚注意到,在输出的开始处有'log4j:WARN记录器没有appender(org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseFactory)。 log4j:WARN请正确初始化log4j系统。它可能是我的问题的根源吗? – ashur