001
002 /*
003 * Copyright (C) 2011 Archie L. Cobbs. All rights reserved.
004 *
005 * $Id: QueryCallback.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
013 import org.springframework.orm.jpa.JpaCallback;
014
015 /**
016 * Helper class for building and executing JPA queries.
017 *
018 * @param <R> query return type
019 */
020 public abstract class QueryCallback<R> implements JpaCallback<R> {
021
022 @Override
023 public final R doInJpa(EntityManager entityManager) {
024 return this.executeQuery(this.buildQuery(entityManager));
025 }
026
027 /**
028 * Build the query.
029 */
030 protected abstract Query buildQuery(EntityManager entityManager);
031
032 /**
033 * Execute the query.
034 */
035 protected abstract R executeQuery(Query query);
036 }
037