Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify programmatic bootstrapping of an EntityManagerFactory #149

Closed
lukasj opened this issue May 22, 2017 · 2 comments · Fixed by #465
Closed

Simplify programmatic bootstrapping of an EntityManagerFactory #149

lukasj opened this issue May 22, 2017 · 2 comments · Fixed by #465
Projects

Comments

@lukasj
Copy link
Contributor

lukasj commented May 22, 2017

Currently, I find it incredibly hard to bootstrap an EntityManagerFactory outside of a container. There are tons of blog posts out there who give advice on how to use PersistenceUnitInfo et al. to do that, but whenever I try these approaches, I'm wasting hours until I finally give up and resort to using some Spring utilities. This is how I can do it with Spring:

LocalContainerEntityManagerFactoryBean bean = new LocalContainerEntityManagerFactoryBean();
HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
adapter.setDatabasePlatform(SQLDialect.H2.thirdParty().hibernateDialect());
bean.setDataSource(new SingleConnectionDataSource(connection, true)); // pre-existing
bean.setPackagesToScan("org.jooq.example.jpa.entity"); // pre-existing
bean.setJpaVendorAdapter(adapter);
bean.setPersistenceUnitName("test");
bean.setPersistenceProviderClass(HibernatePersistenceProvider.class);
bean.afterPropertiesSet();

EntityManagerFactory emf = bean.getObject();

Observe that I have:

  • A pre-existing DataSource or JDBC Connection (that's important, and cannot be done through persistence.xml)
  • Pre-existing entities

That's it. I'm sure this can be done in an even simpler fashion in Spring, or, e.g. through javax.persistence.Persistence in a future JPA version.

Hope this helps

@lukasj
Copy link
Contributor Author

lukasj commented Aug 31, 2018

@gavinking
Copy link
Contributor

Similar issues: #358, #114

gavinking added a commit to gavinking/jpa-api that referenced this issue Aug 13, 2023
gavinking added a commit to gavinking/jpa-api that referenced this issue Aug 13, 2023
@gavinking gavinking linked a pull request Aug 13, 2023 that will close this issue
gavinking added a commit to gavinking/jpa-api that referenced this issue Aug 13, 2023
gavinking added a commit to gavinking/jpa-api that referenced this issue Aug 21, 2023
gavinking added a commit to gavinking/jpa-api that referenced this issue Aug 21, 2023
gavinking added a commit to gavinking/jpa-api that referenced this issue Aug 21, 2023
gavinking added a commit to gavinking/jpa-api that referenced this issue Aug 21, 2023
gavinking added a commit to gavinking/jpa-api that referenced this issue Aug 21, 2023
gavinking added a commit to gavinking/jpa-api that referenced this issue Aug 21, 2023
gavinking added a commit to gavinking/jpa-api that referenced this issue Aug 21, 2023
gavinking added a commit to gavinking/jpa-api that referenced this issue Aug 21, 2023
gavinking added a commit to gavinking/jpa-api that referenced this issue Aug 21, 2023
lukasj pushed a commit that referenced this issue Aug 21, 2023
@lukasj lukasj added this to To do in 3.2.0 via automation Aug 21, 2023
@lukasj lukasj moved this from To do to Done in 3.2.0 Aug 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
3.2.0
Done
Development

Successfully merging a pull request may close this issue.

2 participants