/** * Updates an entity on the database. ALL new reference to the updated entity is returned, the old one should be * discarded. * * @param entity The entity to update * @return ALL new reference to the updated object that should be used for further actions. */ public T merge(T entity) { preMerge(entity); // TODO check access return entityManager.merge(entity); }
/** * Creates an entity on the database. The object is updated with the ID that it is created with. * * @param entity The entity to create */ public void persist(T entity) { prePersist(entity); // TODO check access entityManager.persist(entity); }
/** * Removes an entity from the database. * * @param entity The entity to remove. */ public void remove(T entity) { preRemove(entity); // TODO check access entityManager.remove(entity); }
/** * Returns a type-safe result list which contains all entities of the provided type. * * @return the type-safe result list. */ public List<T> findAll() { CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<T> query = builder.createQuery(entityType); CriteriaQuery<T> all = query.select(query.from(entityType)); return resultList(entityManager.createQuery(all)); }
/** * Deletes an entity on the database without performing the normal user level access check. Should only be used by * sublcasses in special cases where you know exactly what you are doing. * * @param entity The entity to remove */ protected void removeWithoutAccessCheck(T entity) { preRemove(entity); entityManager.remove(entity); }
/** * Returns a type-safe single result of the given query or null. * * @param query Typed query to get result from * @return the result or null * @throws NonUniqueResultException if more than one result */ protected T singleResult(TypedQuery<T> query) { List<T> resultList = resultList(query); if (resultList.isEmpty()) { return null; } if (resultList.size() > 1) { // maybe the result is a join, so make it distinct. Set<T> distinctResult = new HashSet<>(resultList); if (distinctResult.size() > 1) { throw new NonUniqueResultException("Result for query '" + query + "' must contain exactly one item"); } } return resultList.get(0); }
/** * Persists an entity without performing the normal user level access check. Should only be used by subclasses * in special cases where you know exactly what you are doing. * * @param entity The entity to update without access check */ protected void persistWithoutAccessCheck(T entity) { prePersist(entity); entityManager.persist(entity); }
/** * Updates an entity on the database without performing the normal user level access check. Should only be used by * subclasses in special cases where you know exactly what you are doing. * * @param entity The entity to update * @return ALL new reference to the updated object that should be used for further actions. */ protected T mergeWithoutAccessCheck(T entity) { preMerge(entity); return entityManager.merge(entity); }