java - Hsqldb doesn't get populated with data -
i want insert sql data when run datasource configuration can't data in test class. works if create data first inside test class , getting though.
@configuration @enablejparepositories("se.system.repository") public class dbconfig{ @bean(name = "hsqldb") public datasource inmemorydatasource() { embeddeddatabasebuilder builder = new embeddeddatabasebuilder(); embeddeddatabase database = builder .settype(embeddeddatabasetype.hsql) .addscript("classpath:se/system/sql/create-db.sql") .setname("database") .build(); return database; } @bean public jpatransactionmanager transactionmanager(entitymanagerfactory factory) { return new jpatransactionmanager(factory); } @bean public jpavendoradapter jpavendoradapter() { hibernatejpavendoradapter adapter = new hibernatejpavendoradapter(); adapter.setdatabase(database.hsql); adapter.setshowsql(false); adapter.setgenerateddl(true); return adapter; } @bean public localcontainerentitymanagerfactorybean entitymanagerfactory() { localcontainerentitymanagerfactorybean factory = new localcontainerentitymanagerfactorybean(); factory.setdatasource(inmemorydatasource()); factory.setjpavendoradapter(jpavendoradapter()); factory.setpackagestoscan("se.system.model"); return factory; } @bean public resourcedatabasepopulator databasepopulator() { resourcedatabasepopulator populator = new resourcedatabasepopulator(); populator.setsqlscriptencoding("utf-8"); populator.addscript(new classpathresource("se/system/sql/insert-data.sql")); return populator; } @bean public initializingbean populatorexecutor() { return () -> databasepopulatorutils.execute(databasepopulator(), inmemorydatasource()); }
inside testclass:
@beforeclass public static void setup() throws serviceexception { try (annotationconfigapplicationcontext context = new annotationconfigapplicationcontext()) { context.scan("se.system"); context.refresh(); userservice = context.getbean(userservice.class); system.out.println(userservice.getuserbyid(1l)); }
spring data.sql data-${platform}.sql files run. uses spring jdbc this.
try renaming sql file data.sql , adapt method accordingly:
populator.addscript(new classpathresource("se/system/sql/data.sql"));
instead of
populator.addscript(new classpathresource("se/system/sql/insert-data.sql"));
http://docs.spring.io/spring-boot/docs/current/reference/html/howto-database-initialization.html
Comments
Post a Comment