1 /*
2  * Copyright (c) 2008, 2019 Oracle and/or its affiliates. All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v. 2.0 which is available at
6  * http://www.eclipse.org/legal/epl-2.0,
7  * or the Eclipse Distribution License v. 1.0 which is available at
8  * http://www.eclipse.org/org/documents/edl-v10.php.
9  *
10  * SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
11  */

12
13 // Contributors:
14 //     Linda DeMichiel - 2.1
15 //     Linda DeMichiel - 2.0
16
17 package javax.persistence;
18
19 import java.lang.annotation.Target;
20 import java.lang.annotation.Retention;
21 import static java.lang.annotation.RetentionPolicy.RUNTIME;
22
23 /**
24  * Used in conjunction with the {@link SqlResultSetMapping} annotation to map the SELECT
25  * clause of a SQL query to a constructor.
26  *
27  * <p>Applies a constructor for the target class, passing in as arguments
28  * values from the specified columns.  All columns corresponding
29  * to arguments of the intended constructor must be specified using the
30  * <code>columns</code> element of the <code>ConstructorResult</code>
31  * annotation in the same order as that of the argument list of the
32  * constructor.  Any entities returned as constructor results will be
33  * in either the new or detached state, depending on whether a primary
34  * key is retrieved for the constructed object.
35  * 
36  * <pre>
37  *
38  * Example:
39  *
40  *   Query q = em.createNativeQuery(
41  *      "SELECT c.id, c.name, COUNT(o) as orderCount, AVG(o.price) AS avgOrder " +
42  *      "FROM Customer c, Orders o " +
43  *      "WHERE o.cid = c.id " +
44  *      "GROUP BY c.id, c.name",
45  *      "CustomerDetailsResult");
46  *
47  *   &#064;SqlResultSetMapping(
48  *       name="CustomerDetailsResult",
49  *       classes={
50  *          &#064;ConstructorResult(
51  *               targetClass=com.acme.CustomerDetails.class,
52  *                 columns={
53  *                    &#064;ColumnResult(name="id"),
54  *                    &#064;ColumnResult(name="name"),
55  *                    &#064;ColumnResult(name="orderCount"),
56  *                    &#064;ColumnResult(name="avgOrder", type=Double.class)
57  *                    }
58  *          )
59  *       }
60  *      )
61  *
62  * </pre>
63  *
64  * @see SqlResultSetMapping
65  * @see ColumnResult
66  *
67  * @since 2.1
68  */

69 @Target({}) 
70 @Retention(RUNTIME)
71
72 public @interface ConstructorResult { 
73
74     /** (Required) The class whose constructor is to be invoked. */
75     Class targetClass();
76
77     /** 
78      *  (Required) The mapping of columns in the SELECT list to the arguments
79      *  of the intended constructor, in order.
80      */

81     ColumnResult[] columns();
82 }
83