在 EJB 类 之间传递 EntityManager?
Passing EntityManager between EJB classes?
我在 Java EJB
class 中有以下代码调用另一个服务 class 并将 entityManager
作为参数传递:
约会服务 EJB:
try {
entityManager = entityManagement.createEntityManager(client);
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaQuery<Appointment> query = builder.createQuery(Appointment.class);
Root<Appointment> from = query.from(Appointment.class);
CriteriaQuery<Appointment> selectQuery = query.select(from);
searchService.searchByKeywords(searchRequest, searchResults, entityManager, selectQuery, keywordPredicates);
} catch (Exception e) {
}
SearchService EJB:
public List<Appointment> searchByKeywords(AppointmentSearchRequest searchRequest, List<Appointment> searchResults, EntityManager entityManager, CriteriaQuery<Appointment> selectQuery, Optional<List<Predicate>> keywordPredicates) {
Map<Integer, Appointment> uniqueSearchResults = new HashMap<>();
if(keywordPredicates.isPresent()){
for (Predicate singlePredicate : keywordPredicates.get()) {
selectQuery = selectQuery.where(singlePredicate);
List<Appointment> singleQueryResults = entityManager.createQuery(selectQuery).setFirstResult(searchRequest.getIndex()).setMaxResults(searchRequest.getSize()).getResultList();
searchResults.addAll(singleQueryResults);
}
}
return searchResults;
}
最好的做法是在 EJB class 之间传递 entityManager
、selectQuery
,还是我应该在 SearchService EJB
中实例化 entityManager?
最佳做法是将 entityManager
注入任何需要它的 EJB。
@PersistenceContext
private EntityManager entityManager;
容器将搜索 persistence.xml
文件,并在您的 EJB 中注入实体管理器。
可以指定不同的持久化单元,unitName
:
@PersistenceContext(unitName = "unitName")
private EntityManager entityManager;
实体管理器的范围与事务相关,因此参与同一事务的每个 EJB 都将注入相同的 entityManager
。
我在 Java EJB
class 中有以下代码调用另一个服务 class 并将 entityManager
作为参数传递:
约会服务 EJB:
try {
entityManager = entityManagement.createEntityManager(client);
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaQuery<Appointment> query = builder.createQuery(Appointment.class);
Root<Appointment> from = query.from(Appointment.class);
CriteriaQuery<Appointment> selectQuery = query.select(from);
searchService.searchByKeywords(searchRequest, searchResults, entityManager, selectQuery, keywordPredicates);
} catch (Exception e) {
}
SearchService EJB:
public List<Appointment> searchByKeywords(AppointmentSearchRequest searchRequest, List<Appointment> searchResults, EntityManager entityManager, CriteriaQuery<Appointment> selectQuery, Optional<List<Predicate>> keywordPredicates) {
Map<Integer, Appointment> uniqueSearchResults = new HashMap<>();
if(keywordPredicates.isPresent()){
for (Predicate singlePredicate : keywordPredicates.get()) {
selectQuery = selectQuery.where(singlePredicate);
List<Appointment> singleQueryResults = entityManager.createQuery(selectQuery).setFirstResult(searchRequest.getIndex()).setMaxResults(searchRequest.getSize()).getResultList();
searchResults.addAll(singleQueryResults);
}
}
return searchResults;
}
最好的做法是在 EJB class 之间传递 entityManager
、selectQuery
,还是我应该在 SearchService EJB
中实例化 entityManager?
最佳做法是将 entityManager
注入任何需要它的 EJB。
@PersistenceContext
private EntityManager entityManager;
容器将搜索 persistence.xml
文件,并在您的 EJB 中注入实体管理器。
可以指定不同的持久化单元,unitName
:
@PersistenceContext(unitName = "unitName")
private EntityManager entityManager;
实体管理器的范围与事务相关,因此参与同一事务的每个 EJB 都将注入相同的 entityManager
。