Spring Boot and JPA
Entities and EntityManager¶
Spring Boot scans your project's base package for any @Entity classes and adds them to its default Persistence Unit.
The JPA dependency, set when creating the project, causes Spring Boot to create an EntityManager for DAOs to use. The DAO's @PersistenceContext annotation allows Spring Boot to autowire this EntityManager. Spring Boot also creates a TransactionManager for your @Transactional DAOs.
// Example:
package com.example.bootmvc.data;
@Service
@Transactional
public class MyDAOImpl implements MyDAO{
@PersistenceContext
private EntityManager em;
// ...
}
Due to the automatic component scanning, any DAOImpl class annotated with @Service, @Component, or @Repository will have a bean created for it. This bean is eligible to be @Autowired into your Controller classes.
// Example:
package com.example.bootmvc.controllers;
@Controller
public class MyController{
@Autowired
private MyDAO dao;
// ...
}
Finding an @Entity in Different Packages¶
If an @Entity class lives in a package outside your project's base package (e.g. it is in a different Eclipse project), use @EntityScan("other.package.name")
@SpringBootApplication
@EntityScan("com.example.jpavideostore")
public class BootMvcVideoStoreApplication {
public static void main(String[] args) {
SpringApplication.run(BootMvcVideoStoreApplication.class, args);
}
}
Practice Exercise¶
If Spring boot cannot find an entity, the app will still start. When a DAO attempts to query the database, the page and log will display errors like:
org.hibernate.hql.internal.ast.QuerySyntaxException: Film is not mapped
Drill¶
- Add entity scanning configuration to
com.example.bootmvc.BootMvcVideoStoreApplication@SpringBootApplication @EntityScan("com.example.jpavideostore") public class BootMvcVideoStoreApplication { public static void main(String[] args) { SpringApplication.run(BootMvcVideoStoreApplication.class, args); } }