Hibernate JPA @NamedStoredProcedureQuery Example

@NamedStoredProcedureQuery

The @NamedStoredProcedureQuery annotation is used to specify a stored procedure query that can be retrieved later by its name.

NamedStoredProcedureQuery specifies and names a stored procedure, its parameters, and its result type.
The NamedStoredProcedureQuery annotation can be applied to an entity or mapped superclass.

The name element is the name that is passed as an argument to the EntityManager.createNamedStoredProcedureQuery(java.lang.String) method to create an executable StoredProcedureQuery object. Names are scoped to the persistence unit.

The procedureName element is the name of the stored procedure in the database.

The parameters of the stored procedure are specified by the parameters element. All parameters must be specified in the order in which they occur in the parameter list of the stored procedure.

The resultClasses element refers to the class (or classes) that are used to map the results. The resultSetMappings element names one or more result set mappings, as defined by the SqlResultSetMapping annotation.

If there are multiple result sets, it is assumed that they will be mapped using the same mechanism — e.g., either all via a set of result class mappings or all via a set of result set mappings. The order of the specification of these mappings must be the same as the order in which the result sets will be returned by the stored procedure invocation. If the stored procedure returns one or more result sets and no resultClasses or resultSetMappings element is specified, any result set will be returned as a list of type Object[]. The combining of different strategies for the mapping of stored procedure result sets is undefined.

The hints element may be used to specify query properties and hints. Properties defined by this specification must be observed by the provider. Vendor-specific hints that are not recognized by a provider must be ignored.

All parameters of a named stored procedure query must be specified using the StoredProcedureParameter annotation.

Just like with SQL statements, you can also use named queries to call stored procedures. For this purpose, JPA defines the @NamedStoredProcedureQuery annotation.

Oracle REF_CURSOR named query stored procedure

@NamedStoredProcedureQueries(
    @NamedStoredProcedureQuery(
        name = "sp_person_phones",
        procedureName = "sp_person_phones",
        parameters = {
            @StoredProcedureParameter(
                name = "personId",
                type = Long.class,
                mode = ParameterMode.IN
            ),
            @StoredProcedureParameter(
                name = "personPhones",
                type = Class.class,
                mode = ParameterMode.REF_CURSOR
            )
        }
    )
)
Calling this stored procedure is straightforward, as illustrated by the following example.

Calling an Oracle REF_CURSOR stored procedure using a JPA named query

List<Object[]> postComments = entityManager
.createNamedStoredProcedureQuery( "sp_person_phones" )
.setParameter( "personId", 1L )
.getResultList();

Comments

Popular posts from this blog

Today Walkin 14th-Sept

Hibernate Search - Elasticsearch with JSON manipulation

Spring Elasticsearch Operations