001/*
002 * Copyright (c) 2008, 2009, 2011 Oracle, Inc. All rights reserved.
003 *
004 * This program and the accompanying materials are made available under the
005 * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0
006 * which accompanies this distribution.  The Eclipse Public License is available
007 * at http://www.eclipse.org/legal/epl-v10.html and the Eclipse Distribution License
008 * is available at http://www.eclipse.org/org/documents/edl-v10.php.
009 */
010package javax.persistence;
011
012import java.lang.annotation.Retention;
013import java.lang.annotation.Target;
014
015import static java.lang.annotation.RetentionPolicy.RUNTIME;
016
017/**
018 * Used in conjunction with the {@link SqlResultSetMapping} annotation to map the SELECT clause of a SQL query
019 * to a constructor.
020 * <p/>
021 * Applies a constructor for the target class, passing in as arguments values from the specified columns. All
022 * columns corresponding to arguments of the intended constructor must be specified using the {@code columns}
023 * element of the {@code ConstructorResult} annotation in the same order as that of the argument list of the
024 * constructor. Any entities returned as constructor results will be in either the new or detached state,
025 * depending on whether a primary key is retrieved for the constructed object.
026 * <p/>
027 * Example:
028 * <p>
029 * <pre>
030 *     Query q = em.createNativeQuery(
031 *              "SELECT c.id, c.name, COUNT(o) as orderCount, AVG(o.price) AS avgOrder " +
032 *                      "FROM Customer c, Orders o " +
033 *                      "WHERE o.cid = c.id " +
034 *                      "GROUP BY c.id, c.name",
035 *              "CustomerDetailsResult"
036 *    );
037 *
038 *    @SqlResultSetMapping(
039 *              name="CustomerDetailsResult",
040 *              classes = {
041 *                      @ConstructorResult(
042 *                              targetClass=com.acme.CustomerDetails.class,
043 *                              columns={
044 *                                      @ColumnResult(name="id"),
045 *                                      @ColumnResult(name="name"),
046 *                                      @ColumnResult(name="orderCount"),
047 *                                      @ColumnResult(name="avgOrder", type=Double.class)
048 *                          }
049 *                  )
050 *              }
051 * </pre>
052 */
053@Target(value = {})
054@Retention(RUNTIME)
055public @interface ConstructorResult {
056  /**
057   * (Required) The class whose constructor is to be invoked.
058   *
059   * @return target class
060   */
061  Class targetClass();
062
063  /**
064   * (Required) The mapping of columns in the SELECT list to the arguments of the intended constructor, in
065   * order.
066   *
067   * @return columns
068   */
069  ColumnResult[] columns();
070}
071