Bootstrap EJB3 application before JPA / Hibernate

2019-08-09 01:50发布

问题:

I have an EJB3 project (EAR packaging of JARs, no WARs) that I'm trying to integrate Flyway (database migrations) into.

I've tried a couple of options to create a hook where I can run code before Hibernate starts doing its thing (binding to entities etc);

  • Using the @StartUp annotation. This unfortunately gets run AFTER the JPA startup.
  • ServletContextListener / @WebListener. But of course you need a WEB-INF/ directory for that to work.
  • Looked at using the @DependsOn annotation to see if I could get a class to run before Hibernate, but couldn't get that to work.
  • I'm on Hibernate 3.6 at the moment, bumping to Hibernate 4 (and thus getting the Integrator API) is not really an option.

Do I have any other options?

回答1:

I do think that If you do not want to JPA to start you will need to use RESOURCE_LOCAL transaction with non-jta-datasource.

If you use any kind of JTA transactions the JPA will be eagerly invoked.

Like this the JPA will only be invoked when you start a transaction, but you will need to control it manually.