Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2019 1000kit.org.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.tkit.quarkus.jpa.daos;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tkit.quarkus.jpa.exceptions.ConstraintException;
import org.tkit.quarkus.jpa.exceptions.DAOException;
import org.tkit.quarkus.jpa.models.AbstractTraceableEntity;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.persistence.EntityGraph;
import javax.persistence.EntityManager;
import javax.persistence.LockModeType;
import javax.persistence.PersistenceException;
import javax.persistence.Query;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaDelete;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.CriteriaUpdate;
import javax.transaction.Transactional;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;
/**
* The abstract EAO service class using an entity type.
*
* @param the entity class {@link AbstractTraceableEntity}.
*/
public abstract class AbstractDAO> implements EntityService {
/**
* The logger for this class.
*/
private static final Logger log = LoggerFactory.getLogger(AbstractDAO.class);
/**
* The property hint is javax.persistence.loadgraph.
*
* This hint will treat all the specified attributes in the Entity Graph as
* FetchType.EAGER. Attributes that are not specified are treated as
* FetchType.LAZY.
*/
protected static final String HINT_LOAD_GRAPH = "javax.persistence.loadgraph";
/**
* The entity ID attribute.
*/
private static final String ID = "id";
/**
* The entity manager.
*/
@Inject
protected EntityManager em;
/**
* The entity class.
*/
protected Class entityClass;
/**
* The entity name.
*/
protected String entityName;
/**
* Initialize the entity service bean.
*/
@PostConstruct
@SuppressWarnings("unchecked")
public void init() {
String serviceClass = getClass().getName();
entityClass = getEntityClass();
entityName = getEntityName();
log.debug("Initialize the entity service {} for entity {}/{}", serviceClass, entityClass, entityName);
}
/**
* Gets the entity manager.
*
* @return the entity manager.
*/
protected EntityManager getEntityManager() {
return em;
}
/**
* Creates the page query of the DAO {@code } type.
*
* @param query the criteria query
* @param page the page for the query
* @return the new page query instance
*/
public PagedQuery createPageQuery(CriteriaQuery query, Page page) {
return new PagedQuery<>(em, query, page);
}
/**
* Creates the page query of the custom {@code } type
*
* @param query the criteria query
* @param page the page for the query
* @param the entity type of the paged query.
* @return the new page query instance
*/
public PagedQuery createPageQueryCustom(CriteriaQuery query, Page page) {
return new PagedQuery<>(em, query, page);
}
/**
* Finds all entities.
*
* @return the stream of finds entities.
* @throws DAOException if the method fails.
*/
public Stream findAll() throws DAOException {
return findAll(null);
}
/**
* Finds all entities.
*
* @param entityGraph the entity graph.
* @return the list loaded entities.
* @throws DAOException if the method fails.
*/
public Stream findAll(EntityGraph> entityGraph) throws DAOException {
try {
CriteriaQuery cq = criteriaQuery();
cq.from(entityClass);
cq.distinct(true);
TypedQuery query = getEntityManager().createQuery(cq);
if (entityGraph != null) {
query.setHint(HINT_LOAD_GRAPH, entityGraph);
}
return query.getResultStream();
} catch (Exception e) {
throw new DAOException(Errors.FIND_ALL_ENTITIES_FAILED, e, entityName, entityGraph == null ? null : entityGraph.getName());
}
}
/**
* Finds the entity by ID.
*
* @param id the entity ID.
* @return the entity corresponding to the ID.
* @throws DAOException if the method fails.
*/
@Transactional(value = Transactional.TxType.SUPPORTS, rollbackOn = DAOException.class)
public T findById(final Object id) throws DAOException {
try {
return getEntityManager().find(entityClass, id);
} catch (Exception e) {
throw new DAOException(Errors.FIND_ENTITY_BY_ID_FAILED, e, entityName, id);
}
}
/**
* Finds the entity by ID and entity graph name.
*
* @param id the ID.
* @param entityGraph the entity graph.
* @return the entity.
* @throws DAOException if the method fails.
*/
@Transactional(value = Transactional.TxType.SUPPORTS, rollbackOn = DAOException.class)
public T findById(Object id, EntityGraph> entityGraph) throws DAOException {
try {
return getEntityManager().find(entityClass, id, Collections.singletonMap(HINT_LOAD_GRAPH, entityGraph));
} catch (Exception e) {
throw new DAOException(Errors.FIND_ENTITY_BY_ID_FAILED, e, entityName, id, entityGraph == null ? null : entityGraph.getName());
}
}
/**
* Finds the list of object by IDs.
*
* @param ids the set of IDs.
* @return the corresponding list of entities.
* @throws DAOException if the method fails.
*/
@Transactional(value = Transactional.TxType.SUPPORTS, rollbackOn = DAOException.class)
public Stream findByIds(List