SQL query (Note 2--entity query)

Source: Internet
Author: User

SQL query (Note 2--entity query) 

Second, the entity query

If the query returns all of the data columns for a data table, and the data table has a corresponding persisted class map, we convert the query results into entity queries. You can use the multiple overloaded addentity () methods provided by SQLQuery to convert query results to entities.

String sqlString = "SELECT * from enrolment where year=:year";

List L = session.createsqlquery (sqlString)

The record row of the specified query is converted to a enrolment entity

. Addentity (Enrolment.class)

Setting values for parameters of SQL strings

. Setinteger ("Year", 2005)

. List ();

Note: When using native SQL queries, the program must select all columns of data to be converted into persisted entities. Assuming that an entity has a <many-to-one/> pipe to point to another entity when mapping, the foreign key column of the <many-to-one/> map must be returned in the SQL query, or it will cause the "column not found" exception to be thrown. The simplest approach is to use (*) in the SQL string to indicate that all columns are returned.

Hibernate supports converting query results to multiple entities, and if you want to convert query results to multiple entities, you should specify different aliases for different data tables in the SQL string and call Addentity (string alias, Class Entityclass) method to convert different data tables into different entities. The program fragment is as follows:

String sqlString = "Select s.*,e.*,c.* from student s,enrolment e,course c where s.student_id = e.student_id and E.course_ Code = C.course_code ";

List L = session.createsqlquery (sqlString)

. addentity ("s", Student.class)

. addentity ("E", Enrolment.class)

. addentity ("C", Course.class)

Iii. conversion of queries to non-persistent entities

As long as the JavaBean provides the corresponding getter and setter methods for these data columns.

The query interface provides a Setresulttransformer () method that takes a Transformers object and converts the query's result set to a JavaBean set by using the object's method, such as the Aliastobean () method.

SQL query (Note 2--entity query)

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.