0
votes

We have a spring-batch application with DB2 jdbc data source. Want to add Flyway migration capabilities to our app. I've been exploring this article, it makes perfect sense, except the section that mentions how to specify the 'entityManagerFactory' - their example is for JPA with Hibernate,and it looks like this:

<!-- Entity Manager Factory configuration -->
<bean id="entityManagerFactory" class="o.s.orm.jpa.LocalContainerEntityManagerFactoryBean" depends-on="flyway">
    <property name="dataSource" ref="dataSource" />
    <property name="jpaVendorAdapter">          
            <bean class="o.s.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="database" value="${jpa.database}"/>
            </bean>
    </property>
</bean>

Our app is simple JDBC datasource for db2. How can I define that <bean id="entityManagerFactory" so that Spring recognizes it as a managed Bean? Do I even need to specify the entityManagerFactory bean configuration?

1

1 Answers

1
votes

No, you don't have to specify the entityMangerFactory bean. The flyway migrations don't have to be beans. This is an example configuration for flyway migrations:

@Configuration
public class FlywayInitializer {

  @Autowired
  private DataSource dataSource;

  @PostConstruct()
  private void startMigrations() {

    Flyway flyway = new Flyway();
    flyway.setDataSource(dataSource);
    flyway.setLocations("db/migrations");
    flyway.setSchemas("public");
    flyway.setSqlMigrationPrefix("H");
    flyway.migrate();
  }
}

We start by creating a new Flyway object. The javax.Sql.DataSource is the only bean that flyway needs. Flyway needs the data from this bean so it can connect to the database. Then we configure the locations where the migrations are located, the schemas for flyway (the first schema is the one where the schema_version table will be created) and the migration prefix for the migrations (for example, my migrations look like this: H1__init_db.sql).

There are also a lot of other properties that can be set. Once you are done with configuring the flyway object, you call the migrate method in order to execute the migrations.