3

Does spring-boot have a default EntityManager. I am setting one up right now but I noticed when my project loads I see this:

LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'default'

Is this a default EntityManager and if so, how do I access it?

Thank you in advance.

Alert1201
  • 31
  • 1
  • 2

2 Answers2

3

You can use the @PersistenceContext annotation to inject the entity manager into your spring beans:

@PersistenceContext
EntityManager em;
David SN
  • 3,389
  • 1
  • 17
  • 21
1

When using spring-boot-starter-data-jpa, all you need to do is configure the data source using the spring.datasource.{url, username, password, driver-class-name} properties in application.properties.

If you want to use an in-memory database like H2 for development, not even that is necessary. Just include the database as a dependency.

Once you do that, you should be able to inject the EntityManager into your beans.

crizzis
  • 9,978
  • 2
  • 28
  • 47