Hibernate framework-Query result set return type

Source: Internet
Author: User

Hibernate supports HQL and SQL queries, and returns results that support the form of Pojo types or fields/arrays.

Hibernate is used in the development of database queries, using SQL.
I had to query almost all the fields of a table, so I used the Addentity method to convert the query results to a mapped object.
Later requirements change, you need to query several fields of the other tables, do not want to continue to bind the new table with Addentity (after all, only a few fields need to be queried, the extra fields are converted to mapping objects, or only the fields that need to be queried into a mapping object, from a maintenance point of view is not very good), On-line check:
Then use the Addscalar method after addentity to specify the field to return in the specified format
The returned list contains the object[] array, so that in addition to the Addentity mapping object in the array, there is a field result of the specified type

Common result Sets:
First, the use of HQL situation:

    1. Query field: Use "select T.field from Sample T", return list contains object, if multiple individual fields are queried, the returned list contains object[]
      1. list<string> list = Super.gethibernatetemplate (). Find ("SELECT distinct t.job from EMP t order by t.job");

        list<string> list = Super.gethibernatetemplate (). Find ("SELECT distinct t.job from EMP t order by t.job");

    2. Querying a single Entire object: Using the "from sampletable t where T.field =: Filed" notation, default returns the Encapsulated Sampletable object list
    3. Querying multiple Entire objects: You can specify which objects to get, such as "select T1, t2 from sampletable T1, sampletable T2 where ...", which returns a list containing object[],object[] In 0-bit Sampletable object, 1 is Sampletable object
    4. Querying non-mapped objects: You can also "select New Somevo (T.field1, t.field2) from xxx T1" in the constructor method and return the encapsulated Sampletable object list
    5. Querying an entire object and querying a single field: using "Select T, T2.field from Sample T, Sample2 T2", returning the list containing the object[] object, and object[] containing the Sample object, as well as the value of the field type

Second, the case of using sql:

    1. Query fields are similar to the Hql form
    2. Querying a single Entire object, because SQL is used, requires that the program use the Addentity method to specify the relationship between the SQL lookup value and the mapped object (addentity the object type specified must be a mapped object that is already registered)
    3. Querying multiple entire objects, binding with multiple addentity
    4. Querying both the entire object and a single field: After Addentity, the Addscalar method is used to return the list containing the object[] object, and object[] contains the sample object and the value of the field type.

Hibernate framework-Query result set return type

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.