How can I access the EntityManager from an arbitrary bean in Spring Boot? By arbitrary I mean not a service or repository bean. I tried this:
public class CriteriaFinder<T extends IdentityOwner> {
// THIS DOES NOT WORKS
@Autowired
@PersistenceContext
private EntityManager em;
public List<T> find(List<QueryParameter> parameters, Class<T> clazz) {
if(em == null)
{
logger.error("Entity manager is null in finder");
return null;
}
...
}
}
I tried with/without @Autowired and @PersistenceContext annotations and with both.
(I want to use the criteria finder with containment to implement custom find)
In a custom @Repository implementation I can access the EntityManager:
public class CustomerRepositoryImpl implements CustomerFinderRepository {
// THIS WORKS
@PersistenceContext
private EntityManager em;
...
}
I assume it has something to do with the way Spring is finding and initializing beans. I would also like to avoid passing the EntityManager with a constructor or setter.