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.
@Configuration
public class JPAQueryFactoryConfiguration {
@Bean
public JPAQueryFactory jpaQueryFactory(@Autowired EntityManager entityManager) {
return new JPAQueryFactory(entityManager);
}
}
@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
i am just biginer in EJB , type of JPAQueryFactory is Provider is not EntityManager , just Cast
JPAQueryFactory query = new JPAQueryFactory((Provider<EntityManager>) em);
or use the declaration of JPAQueryFactory
EntityManagerFactory emf =
Persistence.createEntityManagerFactory("com.baeldung.querydsl.intro");
EntityManager em = entityManagerFactory.createEntityManager();
JPAQueryFactory queryFactory = new JPAQueryFactory(em);
This article show how to use both entity manager and JPAQueryFactory: https://dzone.com/articles/spring-data-jpa-querydsl-taking-best-from-both-wor
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.