Hibernate uses native SQL in naming query named queries

Source: Internet
Author: User
Tags scalar
13.2. Named SQL queries

Named SQL queries may be defined in the mapping document and called in exactly the same way as a named hql query. In this case, we doNotNeed to callAddentity ().

<sql-query name="persons">
<return alias="person" class="eg.Person"/>
SELECT person.NAME AS {person.Name},
person.AGE AS {person.Age},
person.SEX AS {person.Sex}
FROM PERSON person
WHERE person.NAME LIKE :namePattern
</sql-query>
IList people = sess.GetNamedQuery("persons")
.SetString("namePattern", namePattern)
.SetMaxResults(50)
.List();

The<Return-join>And<Load-collection>Elements are used to join associations and define queries which initialize collections, respectively.

<sql-query name="personsWith">
<return alias="person" class="eg.Person"/>
<return-join alias="address" property="person.MailingAddress"/>
SELECT person.NAME AS {person.Name},
person.AGE AS {person.Age},
person.SEX AS {person.Sex},
adddress.STREET AS {address.Street},
adddress.CITY AS {address.City},
adddress.STATE AS {address.State},
adddress.ZIP AS {address.Zip}
FROM PERSON person
JOIN ADDRESS adddress
ON person.ID = address.PERSON_ID AND address.TYPE='MAILING'
WHERE person.NAME LIKE :namePattern
</sql-query>

A named SQL query may return a scalar value. You must declare the column alias and nhib1_type using<Return-scalar>Element:

<sql-query name="mySqlQuery">
<return-scalar column="name" type="String"/>
<return-scalar column="age" type="Int64"/>
SELECT p.NAME AS name,
p.AGE AS age,
FROM PERSON p WHERE p.NAME LIKE 'Hiber%'
</sql-query>

You can externalize the resultset mapping iions in<Resultset>Element to either reuse them accross several named queries or throughSetresultsetmapping ()API.

<resultset name="personAddress">
<return alias="person" class="eg.Person"/>
<return-join alias="address" property="person.MailingAddress"/>
</resultset>

<sql-query name="personsWith" resultset-ref="personAddress">
SELECT person.NAME AS {person.Name},
person.AGE AS {person.Age},
person.SEX AS {person.Sex},
adddress.STREET AS {address.Street},
adddress.CITY AS {address.City},
adddress.STATE AS {address.State},
adddress.ZIP AS {address.Zip}
FROM PERSON person
JOIN ADDRESS adddress
ON person.ID = address.PERSON_ID AND address.TYPE='MAILING'
WHERE person.NAME LIKE :namePattern
</sql-query>

You can alternatively use the resultset mapping information in your. HBM. xml files directly in code.

IList cats = sess.CreateSQLQuery(
"select {cat.*}, {kitten.*} from cats cat, cats kitten where kitten.mother = cat.id"
)
.SetResultSetMapping("catAndKitten")
.List();
13.2.1. Using return-property to explicitly specify column/alias names

With<Return-property>You can explain icitly tell Nhibernate what column aliases to use, instead of using{}-Syntax to let Nhibernate inject its own aliases.

<sql-query name="mySqlQuery">
<return alias="person" class="eg.Person">
<return-property name="Name" column="myName"/>
<return-property name="Age" column="myAge"/>
<return-property name="Sex" column="mySex"/>
</return>
SELECT person.NAME AS myName,
person.AGE AS myAge,
person.SEX AS mySex,
FROM PERSON person WHERE person.NAME LIKE :name
</sql-query>

<Return-property>Also works with multiple columns. This solves a limitation with{}-Syntax which can not allow fine grained control of Multi-column properties.

<sql-query name="organizationCurrentEmployments">
<return alias="emp" class="Employment">
<return-property name="Salary">
<return-column name="VALUE"/>
<return-column name="CURRENCY"/>
</return-property>
<return-property name="EndDate" column="myEndDate"/>
</return>
SELECT EMPLOYEE AS {emp.Employee}, EMPLOYER AS {emp.Employer},
STARTDATE AS {emp.StartDate}, ENDDATE AS {emp.EndDate},
REGIONCODE as {emp.RegionCode}, EID AS {emp.Id}, VALUE, CURRENCY
FROM EMPLOYMENT
WHERE EMPLOYER = :id AND ENDDATE IS NULL
ORDER BY STARTDATE ASC
</sql-query>

Notice that in this example we used<Return-property>In combination with{}-Syntax for injection, allowing 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.

13.2.2. Using Stored Procedures for querying

Nhibernate 1.2 introduces 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 to be able to work with nhib.pdf. an example of such a stored function in ms SQL Server 2000 and higher is as follows:

CREATE PROCEDURE selectAllEmployments AS
SELECT EMPLOYEE, EMPLOYER, STARTDATE, ENDDATE,
REGIONCODE, EMPID, VALUE, CURRENCY
FROM EMPLOYMENT

To use this query in Nhibernate you need to map it via a named query.

<sql-query name="selectAllEmployments_SP">
<return alias="emp" class="Employment">
<return-property name="employee" column="EMPLOYEE"/>
<return-property name="employer" column="EMPLOYER"/>
<return-property name="startDate" column="STARTDATE"/>
<return-property name="endDate" column="ENDDATE"/>
<return-property name="regionCode" column="REGIONCODE"/>
<return-property name="id" column="EID"/>
<return-property name="salary">
<return-column name="VALUE"/>
<return-column name="CURRENCY"/>
</return-property>
</return>
exec selectAllEmployments
</sql-query>

Notice that stored procedures currently only return scalars and entities.<Return-join>And<Load-collection>Are not supported.

13.2.2.1. Rules/limitations for using Stored Procedures

To use stored procedures with Nhibernate the procedures/functions have to follow some rules. if they do not follow those rules they are not usable with nhib.pdf. if you still want to use these procedures you have to execute themSession. Connection. The rules are different for each database, since database vendors have different stored procedure semantics/syntax.

Stored Procedure queries can't be pagedSetfirstresult ()/setmaxresults ().

Recommended call form is dependent on your database. For ms SQL server useExec functionname <parameters>.

For Oracle the following rules apply:

  • A function must return a result set. The first parameter of a procedure must beOutThat returns a result set. This is done by usingSys_refcursorType in Oracle 9 or 10. in Oracle you need to defineRef cursorType, see Oracle literature.

For ms SQL Server the following rules apply:

  • The procedure must return a result set. Nhibernate will useIdbcommand. executereader ()To obtain the results.

  • If you can enableSet nocount onIn your procedure it will probably be more efficient, but this is not a requirement.

Refer:
Http://www.hibernate.org/hib_docs/nhibernate/1.2/reference/en/html/querysql.html

Http://blog.csdn.net/cm4ever/archive/2005/04/05/337533.aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.