added find method with support to init certain collections

This commit is contained in:
2019-06-10 14:44:37 +02:00
parent 212e4dad5d
commit 7a380dc214

View File

@ -18,6 +18,7 @@ package de.muehlencord.shared.db;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
@ -26,6 +27,7 @@ import javax.ejb.LockType;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.inject.Inject;
import javax.persistence.EntityGraph;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
@ -229,6 +231,20 @@ public abstract class AbstractController<T> {
return em.find(entityClass, id);
}
@Lock(LockType.READ)
public T find(Object id, String... subGraphItems) {
EntityGraph graph = this.em.createEntityGraph(entityClass);
for (String subGraphItem : subGraphItems) {
graph.addSubgraph(subGraphItem);
}
Map hints = new HashMap<>();
hints.put("javax.persistence.loadgraph", graph);
T entity = (T) em.find(entityClass, id, hints);
return entity;
}
@Lock(LockType.READ)
public List<T> findAll() {
return findAll(new ArrayList<>());