Question

I would like to know the value of the hbm2ddl feature during the runtime of my application. I succeeded to determine that value with the following code:

@Autowired
private ApplicationContext applicationContext;

private getHbm2DdlValue() {
    LocalSessionFactoryBean sessionFactoryBean = (LocalSessionFactoryBean) 
            applicationContext.getBean("&sessionFactory");
    Configuration conf = sessionFactoryBean.getConfiguration();
    String hbm2ddlValue = conf.getProperty("hibernate.hbm2ddl.auto");
    return hbm2ddlValue;
}

Now, I want to know that value before the automatic update is triggered. The problem with the solution above is, that retrieving the LocalSessionFactoryBean immediately triggers the automatic update.

I could read the configuration file from disk and interpret it - but I would rather like to let the configuration build by hibernate and then read the property from there.

Is there any other way to create or access the hibernate configuration without triggering the automatic schema update?

Was it helpful?

Solution

As far as I know, it is not possible to disable factory-initialization during ApplicationContext loading. The LocalSessionFactoryBean is a FactoryBean. Spring will call that object's getObject() method during initialization of the context and that will force the schema to be updated.

Note that the Configuration instance of the LocalSessionFactoryBean is configured and used directly in the afterPropertiesSet() method. You won't be able to intercept it either.

You'll need to create your own Configuration instance (or parse the file yourself) before the ApplicationContext is created. You can look at the source code of LocalSessionFactoryBean to see how that is done.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top