Atitit. The ORM Framework hibernate uses sqlquery createsqlquery addentity

Source: Internet
Author: User
Tags one table

Atitit. The ORM Framework hibernate uses sqlquery createsqlquery addentity

1. addentity
to nativeSQLthe control of query execution is throughsqlqueryinterface, by executing theSession.Createsqlquery ()gets the interface. In the simplest case, we can take the following form:

List cats = Sess.createsqlquery ("Select * from Cats"). Addentity (Cat. Class). list ();

Author :: Old Wow's paw attilax ayron, email:[email protected]

Reprint please indicate source: Http://blog.csdn.net/attilax

2. Addentity placeholder Aliases

Here, the result set field name is assumed to be the same as the field name indicated in the mapping file. For queries that have multiple tables connected, this can cause problems because fields with the same name may appear in more than one table. The following method avoids the problem of duplicate field names :

List cats = Sess.createsqlquery ("Select {cat.*} from Cats Cat"). Addentity ("Cat", Cat. Class). list ();

This query specifies the following:

SQL query statement with a placeholder that allows Hibernate to use the alias of the field .

The query returns an alias for the entity, and its SQL table .

The Addentity () method ties the alias of the SQL table to the entity class and determines the pattern of the query result set.

3. AddJoin ()methods can be used to load the associations of other entities and collections.

List cats = Sess.createsqlquery (
"Select {cat.*}, {kitten.*} from Cats cat, cats kitten where kitten.mother = Cat.id")
. Addentity ("Cat", Cat. Class)
. AddJoin ("Kitten", "Cat.kittens")
. List ();

4. Reference

Hibernate in createsqlquery addentity () and Setresulttransformer () method - The rookie of Daniel -Iteye technology website . htm

Atitit. The ORM Framework hibernate uses sqlquery createsqlquery addentity

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.