2013-08-30 2 views
0

Я пытаюсь запустить HSQL со следующими двумя скриптами, а затем проверить его, но каждый раз, когда я получаю эту ошибку, с которой я не могу справиться.Spring: HSQL- Не удалось выполнить сценарий базы данных

Не удалось выполнить сценарий базы данных; Вложенное исключение - org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Не удалось выполнить оператор SQL-скрипта в строке 4 ресурса пути ресурса ресурса [data.sql]: вставить в значения spittle (spitter_id, spittleText, publishedTime) (2, Тестируем 'новый язык выражений в' Spring '2010-06-11')

Update - дополнительное исключение, которое было брошено: нарушение ограничения

целостности: внешний ключ нет родитель; 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> 

UnitTest

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(); 
    } 


} 
+0

@SotiriosDelimanolis Получало заявление выглядит следующим образом 'SQL_SELECT_SPITTER = "выберите идентификатор, имя пользователя, FULLNAME из Плевальщицы где ID =?"'. Но я тестировал приложение, и он терпит неудачу в методе 'setUp()' (прокомментировал другой тоже). – ashur

+0

@SotiriosDelimanolis Можете ли вы уточнить? Я не уверен, что я должен удалить. Я обновил вопросы со вторым исключением, которое было брошено. – ashur

+0

@SotiriosDelimanolis Это не помогло. Но я только что заметил, что при начале вывода есть «log4j: WARN». Для журнала не найдено ни одного приложения (org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseFactory). log4j: WARN Пожалуйста, правильно инициализируйте систему log4j. «Может ли это быть источником моей проблемы? – ashur

ответ

1

Сценарий начинается значение столбца ID с 0. Вы можете указать начальное значение.

create table spitter (
    id int generated by default as identity (start with 1) primary key, 
    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 
); 

В качестве альтернативы вы можете вставить идентификаторы в явном виде:

insert into spitter (id, username, password, fullname, email, update_by_email) values (1, 'habuma', 'password', 'Craig Walls', '[email protected]', false); 
Смежные вопросы