Is there any way to get a handle to the EntityManager for a given entity object? I\'m using spring boot 1.2.3 with JPA starter and i\'m further explicitly configuring multiple d
It depends how you've been configuring this but have you tried to inject the EntityManager
with a qualifier that corresponds to the factory that created it?
Here is a sample project with two data sources. If you want to inject the EntityManager
for order, just do the following in any Spring bean of the project
@Service
public class FooService {
private final EntityManager entityManager;
@Autowired
public FooService(@Qualifier("orderEntityManager") EntityManager entityManager) {
...
}
}
For customer, use the customerEntityManager
.
Of course you can use the persistent unit name instead, that is
@PersistenceContext(unitName = "customers")
private EntityManager entityManager;
@PersistenceContext(unitName = "orders")
private EntityManager entityManager;
Check the configuration of the project for more details.