All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.mmm.persistence.impl.jpa.query.SimpleQueryImpl Maven / Gradle / Ivy

/* Copyright (c) The m-m-m Team, Licensed under the Apache License, Version 2.0
 * http://www.apache.org/licenses/LICENSE-2.0 */
package net.sf.mmm.persistence.impl.jpa.query;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;

import net.sf.mmm.persistence.api.query.SimpleQuery;

/**
 * This is the implementation of {@link SimpleQuery} using JPA.
 * 
 * @param  is the generic type of the {@link #getSingleResult() result}.
 * 
 * @author Joerg Hohwiller (hohwille at users.sourceforge.net)
 * @since 0.9.0
 */
public class SimpleQueryImpl extends AbstractStatementImpl implements SimpleQuery {

  /** @see #getResultType() */
  private Class resultType;

  /** @see #getOrCreateQuery() */
  private TypedQuery query;

  /**
   * The constructor.
   * 
   * @param jpqlStatement - see {@link #getJpqlStatement()}.
   * @param resultType - see {@link #getResultType()}.
   * @param context is the {@link AbstractJpqlContext} to use as template.
   */
  public SimpleQueryImpl(String jpqlStatement, Class resultType, AbstractJpqlContext context) {

    super(jpqlStatement, context);
    this.resultType = resultType;
  }

  /**
   * The constructor.
   * 
   * @param jpqlStatement - see {@link #getJpqlStatement()}.
   * @param resultType - see {@link #getResultType()}.
   * @param entityManager - see {@link #getParameters()}.
   * @param parameters - see {@link #getParameters()}.
   */
  public SimpleQueryImpl(String jpqlStatement, Class resultType, EntityManager entityManager,
      List parameters) {

    super(jpqlStatement, entityManager, parameters);
    this.resultType = resultType;
  }

  /**
   * @return the resultType
   */
  public Class getResultType() {

    return this.resultType;
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public TypedQuery getOrCreateQuery() {

    if (this.query == null) {
      this.query = createTypedQuery(getJpqlStatement(), this.resultType);
    }
    return this.query;
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public RESULT getSingleResult() throws RuntimeException {

    return getOrCreateQuery().getSingleResult();
  }

}