Liquibase integrated with a Tapestry and Hibernate: initial schema creation step

安稳与你 提交于 2019-12-11 07:38:09

问题


I have the project based on Tapestry framework. It uses Hibernate as ORM library.

My classes are mapped to database using annotations.

I would like to integrate Liquibase into my project to be able to maintance database state, doing version updates.

What I have done till this moment is the following steps:

1) I've created service pair: LiquibaseService -> LiquibaseServiceImpl.

LiquibaseSerivce has method public void update(), which creates independent DataSource from hibernate.cfg.xml configuration and finally does liquibase.update("production");

2) I've added binding to this service in AppModule:

binder.bind(LiquibaseService.class, LiquibaseServiceImpl.class).eagerLoad();

3) I've added initMyApplication method to AppModule which starts Liquibase update:

@Startup
public static void initMyApplication(Logger logger, LiquibaseService liquibaseService) {
    logger.info("Updating database by liquibase service...");
    liquibaseService.update();
    logger.info("update-db done.");
}

All this works fine on production, where initial scheme was already created: I can comfortable drop tables, columns and so on.

But the issue is that I can not create new scheme from zero (using Liquibase) when deploying application to a new server: Hibernate starts independently ans complains, that there aren't any tables mapped to my class in the scheme.

How can I slow down Hibernate starting until that moment when Liquibase will finish its job?


回答1:


Instead of using @Startup you can do something like this

    public static void contributeRegistryStartup(
     final Logger logger, final LiquibaseService liquibaseService,
     OrderedConfiguration<Runnable> configuration)
    {
        configuration.add("Liquibase", new Runnable()
        {
            public void run()
            {
                logger.info("Updating database by liquibase service...");
                liquibaseService.update();
                logger.info("update-db done.");
            }
        }, "after:HibernateStartup");
    }


来源:https://stackoverflow.com/questions/14375949/liquibase-integrated-with-a-tapestry-and-hibernate-initial-schema-creation-step

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!