이 콘텐츠는 선택한 언어로 제공되지 않습니다.
16.2. Named SQL queries
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.2.1. Using return-property to explicitly specify column/alias names
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.