java - Run data scripts after database build -



java - Run data scripts after database build -

so i'm surprised reply wasn't found, want insert info after database gets generated.

rootconfig.java:

... @bean public datasource datasource() throws sqlexception { embeddeddatabasebuilder builder = new embeddeddatabasebuilder(); homecoming builder.settype(embeddeddatabasetype.hsql) .setname("db") .addscript("setup_data.sql") .continueonerror(true) .build(); } @bean public entitymanagerfactory entitymanagerfactory() throws sqlexception { eclipselinkjpavendoradapter vendoradapter = new eclipselinkjpavendoradapter(); vendoradapter.setgenerateddl(true); vendoradapter.setshowsql(true); vendoradapter.setdatabase(database.hsql); localcontainerentitymanagerfactorybean mill = new localcontainerentitymanagerfactorybean(); factory.setjpavendoradapter(vendoradapter); map<string, object> props = new hashmap<>(); props.put("eclipselink.weaving", "false"); props.put("eclipselink.target-database", hsqlplatform.class.getname()); props.put("eclipselink.cache.shared.default", "false"); props.put("eclipselink.logging.parameters", "true"); props.put("eclipselink.logging.level", "finest"); props.put("eclipselink.logging.level.sql", "finest"); props.put("eclipselink.logging.level.cache", "finest"); factory.setjpapropertymap(props); factory.setpackagestoscan("com.citysports.leaguesports.domain"); factory.setdatasource(datasource()); factory.afterpropertiesset(); homecoming factory.getobject(); } ...

i generating ddl, when addscript('setup_data.sql') error because hasn't generated tables yet. how have script run after ddl generation?

you can utilize databasepopulator. so, set next bean definitions in configuration class.

@bean public resourcedatabasepopulator databasepopulator() { resourcedatabasepopulator populator = new resourcedatabasepopulator(); populator.setsqlscriptencoding("utf-8"); populator.addscript(new classpathresource("setup_data.sql")); homecoming populator; } @bean public initializingbean populatorexecutor() { homecoming new initializingbean() { @override public void afterpropertiesset() throws exception { databasepopulatorutils.execute(databasepopulator(), datasource()); } }; }

if you're using java 8, can simplify initializingbean definition form using lambdas:

@bean public initializingbean populatorexecutor() { homecoming () -> databasepopulatorutils.execute(databasepopulator(), datasource()); }

basically define populator has scripts want execute , initializingbean takes care of running scripts when info source bean ready.

hope solution works you

java spring hibernate jpa eclipselink

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -