简体   繁体   中英

Spring / Hibernate: how to get Configuration from LocalContainerEntityManagerFactoryBean

I'd like to run Configuration.generateSchemaUpdateScriptList() . However Hibernate is configured using LocalContainerEntityManagerFactoryBean . How do I get to the Configuration object from a LocalContainerEntityManagerFactoryBean instance? Or is there a better way to achieve this?

OK this was a two-days wall vs. head marathon, but in the end I found a solution, although it's somewhat hacky.

I cam across Hibernate's Integrator , which is scarcely mentioned in the docs. Some more information about it can be found in the linked Hibernate JIRA issues HHH-5562 and HHH-6081 . However, one can piece together enough information for a working example from a few sources once you know the keyword. Still it does not play well with Spring, a problem mentioned in this SO question .

So, the final solution was a bit hackish, but it works:

public class GetConfigIntegrator implements Integrator {

    private static Configuration configuration;

    public static Configuration getConfiguration() {
        return configuration;
    }

    @Override
    public void integrate(Configuration configuration, /* ... */) {
        GetConfigIntegrator.configuration = configuration;
    }

    // Empty Integrator implementation...
}

As can be seen, this silly integrator simply stores the configuration in a static variable, so it can be accessed later. Not ideal, but faced with no options one rejoices finding such a path.

To register the integrator, the following file needs to be created, with one line per integrator class (full name), like so:

# src/main/resources/META-INF/services/org.hibernate.integrator.spi.Integrator:
<full package name>.GetConfigIntegrator

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM