I heard about EntityManager is not thread-safe, so we use @PersistenceContext instead of @Autowired.
However, I found about two ways to create JPAQueryFactory on the Internet.
- define a bean like this
@Configuration
public class JPAQueryFactoryConfiguration {
@Bean
public JPAQueryFactory jpaQueryFactory(@Autowired EntityManager entityManager) {
return new JPAQueryFactory(entityManager);
}
}
- @PersistenceContext on EntityManager ,new JPAQueryFactory(entityManager) every time
@Service
public class TestServiceImpl implements TestService {
@PersistenceContext
private EntityManager entityManager;
@Override
public void test(){
JPAQueryFactory jpaQueryFactory = new JPAQueryFactory(entityManager);
xxxxxxxxxxxx
}
}
I'm counfused. But, I think the first way is not correct according to the difference between @PersistenceContext and @Autowired