【问题标题】:Run data scripts after database build数据库构建后运行数据脚本
【发布时间】:2014-12-05 00:06:21
【问题描述】:

所以我很惊讶这个问题的答案不容易找到,但我想在数据库生成后插入一些数据。

RootConfig.java:

...
    @Bean
    public DataSource dataSource() throws SQLException {
        EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
        return 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 factory = 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();

        return factory.getObject();
    }
...

我正在生成 ddl,但是当我 addScript('setup_data.sql') 时出现错误,因为它尚未生成表。如何在 ddl 生成后运行脚本?

【问题讨论】:

    标签: java spring hibernate jpa eclipselink


    【解决方案1】:

    您可以使用DatabasePopulator。为此,请将以下 bean 定义放入您的配置类中。

    @Bean
    public ResourceDatabasePopulator databasePopulator() {
        ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
        populator.setSqlScriptEncoding("UTF-8");
        populator.addScript(new ClassPathResource("setup_data.sql"));
        return populator;
    }
    
    @Bean
    public InitializingBean populatorExecutor() {
        return new InitializingBean() {
            @Override
            public void afterPropertiesSet() throws Exception {
                DatabasePopulatorUtils.execute(databasePopulator(), dataSource());
            }
        };
    }
    

    如果您使用的是 Java 8,则可以使用 lambda 将 InitializingBean 定义简化为这种形式:

    @Bean
    public InitializingBean populatorExecutor() {
        return () -> DatabasePopulatorUtils.execute(databasePopulator(), dataSource());
    }
    

    基本上,您定义一个填充器,其中包含您要执行的脚本,InitializingBean 负责在数据源 bean 准备好时运行这些脚本。

    希望这个解决方案适合你

    【讨论】:

    • 有无弹簧解决方案吗?
    猜你喜欢
    • 1970-01-01
    • 2022-01-11
    • 1970-01-01
    • 2010-11-19
    • 1970-01-01
    • 1970-01-01
    • 2011-02-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多