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.
package de.taimos.dvalin.jpa;
/*
* #%L
* Hibernate DAO for Spring
* %%
* Copyright (C) 2013 - 2015 Taimos GmbH
* %%
* 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.
* #L%
*/
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityNotFoundException;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import org.springframework.transaction.annotation.Transactional;
/**
* @param the entity type
* @param the id type
* @author hoegertn
*/
public abstract class EntityDAOHibernate, I> implements IEntityDAO {
@PersistenceContext
protected EntityManager entityManager;
@Override
@Transactional
public E save(final E element) {
return this.entityManager.merge(element);
}
@Override
@Transactional
public void delete(final E element) {
this.entityManager.remove(this.entityManager.merge(element));
}
@Override
@Transactional
public void deleteById(final I id) {
final E element = this.findById(id);
if (element == null) {
throw new EntityNotFoundException();
}
this.entityManager.remove(element);
}
@Override
public E findById(final I id) {
return this.entityManager.find(this.getEntityClass(), id);
}
@Override
public List findList(final int first, final int max) {
final TypedQuery query = this.entityManager.createQuery(this.getFindListQuery(), this.getEntityClass());
if (first >= 0) {
query.setFirstResult(first);
}
if (max >= 0) {
query.setMaxResults(max);
}
return query.getResultList();
}
@Override
public List findList() {
return this.findList(-1, -1);
}
/**
* @param query the JPA query
* @param params the positional parameters
* @return the entity or null if not found or more than one found
*/
protected final E findByQuery(final String query, final Object... params) {
final List list = this.findListByQuery(query, params);
if (list.size() == 1) {
return list.get(0);
}
return null;
}
/**
* @param query the JPA query
* @param params the positional parameters
* @return the entity or null if not found or more than one found
*/
protected final Object[] findGenericByQuery(final String query, final Object... params) {
final List