001    
002    /*
003     * Copyright (C) 2011 Archie L. Cobbs. All rights reserved.
004     *
005     * $Id: TypedQueryCallback.java 275 2012-02-13 21:56:57Z archie.cobbs $
006     */
007    
008    package org.dellroad.stuff.dao;
009    
010    import javax.persistence.EntityManager;
011    import javax.persistence.Query;
012    import javax.persistence.TypedQuery;
013    
014    /**
015     * Helper class for building and executing JPA typed queries.
016     *
017     * @param <T> persistent instance type
018     * @param <R> query return type
019     */
020    public abstract class TypedQueryCallback<T, R> extends QueryCallback<R> {
021    
022        /**
023         * Delegates to {@link #executeQuery(TypedQuery)}.
024         */
025        @SuppressWarnings("unchecked")
026        protected final R executeQuery(Query query) {
027            return this.executeQuery((TypedQuery<T>)query);
028        }
029    
030        /**
031         * Build the typed query.
032         */
033        @Override
034        protected abstract TypedQuery<T> buildQuery(EntityManager entityManager);
035    
036        /**
037         * Execute the query.
038         */
039        protected abstract R executeQuery(TypedQuery<T> query);
040    }
041