org.reusables.access.AbstractEntityManagerRepository.java Source code

Java tutorial

Introduction

Here is the source code for org.reusables.access.AbstractEntityManagerRepository.java

Source

/*
 * Copyright 2010-2012 the original author or authors.
 *
 * 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.reusables.access;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import org.apache.commons.lang.Validate;

/**
 * Abstract implementation, using an {@link EntityManager}, for the {@link GeneralRepository} interface
 * offering a number a standard methods generic for most repostories.
 * @author marcel
 * @param <T> Entity type for this repository.
 **/
public abstract class AbstractEntityManagerRepository<T> implements GeneralRepository<T> {
    private final Class<T> entityClass;

    @SuppressWarnings("unchecked")
    protected AbstractEntityManagerRepository() {
        this.entityClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass())
                .getActualTypeArguments()[0];
    }

    /**
     * @return The entity manager.
     */
    protected abstract EntityManager getEm();

    @Override
    public T getById(final Serializable id) {
        final T entity = getEm().find(this.entityClass, id);
        Validate.notNull(entity, "No entity found with id: " + id);
        return entity;
    }

    @Override
    public List<T> findAll(final String orderByPropertyName) {
        final CriteriaBuilder bld = getEm().getCriteriaBuilder();
        final CriteriaQuery<T> query = bld.createQuery(this.entityClass);
        final Root<T> root = query.from(this.entityClass);
        if (orderByPropertyName != null) {
            query.orderBy(bld.asc(root.get(orderByPropertyName)));
        }
        return getEm().createQuery(query).getResultList();
    }

    @Override
    public void save(final T entity) {
        getEm().persist(entity);
    }

    @Override
    public void delete(final T entity) {
        getEm().remove(entity);
    }

    @Override
    public int deleteById(final Serializable id) {
        return deleteById(id, DEFAULT_ID);
    }

    @Override
    public int deleteById(final Serializable id, final String idPropertyName) {
        final String qlString = String.format("DELETE %s WHERE %s = :id", this.entityClass.getName(),
                idPropertyName);
        final Query query = getEm().createQuery(qlString);
        query.setParameter("id", id);
        return query.executeUpdate();
    }
}