Este contenido no está disponible en el idioma seleccionado.
16.3. Named SQL Queries
16.3.1. About Named SQL Queries Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
Named SQL queries can be defined in the mapping document and called in exactly the same way as a named HQL query. In this case, you do not need to call
addEntity()
.
List people = sess.getNamedQuery("persons") .setString("namePattern", namePattern) .setMaxResults(50) .list();
List people = sess.getNamedQuery("persons")
.setString("namePattern", namePattern)
.setMaxResults(50)
.list();
The
<return-join>
element is use to join associations and the <load-collection>
element is used to define queries which initialize collections,
A named SQL query may return a scalar value. You must declare the column alias and Hibernate type using the
<return-scalar>
element:
You can externalize the resultset mapping information in a
<resultset>
element which will allow you to either reuse them across several named queries or through the setResultSetMapping()
API.
You can, alternatively, use the resultset mapping information in your hbm files directly in java code.
List cats = sess.createSQLQuery( "select {cat.*}, {kitten.*} from cats cat, cats kitten where kitten.mother = cat.id" ) .setResultSetMapping("catAndKitten") .list();
List cats = sess.createSQLQuery(
"select {cat.*}, {kitten.*} from cats cat, cats kitten where kitten.mother = cat.id"
)
.setResultSetMapping("catAndKitten")
.list();
16.3.2. Using return-property to Explicitly Specify Column/Alias Names Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
You can explicitly tell Hibernate what column aliases to use with
<return-property>
, instead of using the {}
-syntax to let Hibernate inject its own aliases.For example:
<return-property>
also works with multiple columns. This solves a limitation with the {}
-syntax which cannot allow fine grained control of multi-column properties.
In this example
<return-property>
was used in combination with the {}
-syntax for injection. This allows users to choose how they want to refer column and properties.
If your mapping has a discriminator you must use
<return-discriminator>
to specify the discriminator column.
16.3.3. Using Stored Procedures for Querying Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
Hibernate3 provides support for queries via stored procedures and functions. Most of the following documentation is equivalent for both. The stored procedure/function must return a resultset as the first out-parameter to be able to work with Hibernate. An example of such a stored function in Oracle 9 and higher is as follows:
To use this query in Hibernate you need to map it via a named query.
Stored procedures currently only return scalars and entities.
<return-join>
and <load-collection>
are not supported.
16.3.4. Rules/limitations for Using Stored Procedures Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
You cannot use stored procedures with Hibernate unless you follow some procedure/function rules. If they do not follow those rules they are not usable with Hibernate. If you still want to use these procedures you have to execute them via
session.connection()
. The rules are different for each database, since database vendors have different stored procedure semantics/syntax.
Stored procedure queries cannot be paged with
setFirstResult()/setMaxResults()
.
The recommended call form is standard SQL92:
{ ? = call functionName(<parameters>) }
or { ? = call procedureName(<parameters>}
. Native call syntax is not supported.
For Oracle the following rules apply:
- A function must return a result set. The first parameter of a procedure must be an
OUT
that returns a result set. This is done by using aSYS_REFCURSOR
type in Oracle 9 or 10. In Oracle you need to define aREF CURSOR
type. See Oracle literature for further information.
For Sybase or MS SQL server the following rules apply:
- The procedure must return a result set. Note that since these servers can return multiple result sets and update counts, Hibernate will iterate the results and take the first result that is a result set as its return value. Everything else will be discarded.
- If you can enable
SET NOCOUNT ON
in your procedure it will probably be more efficient, but this is not a requirement.