Hibernate SQL queries converted to entity classes

Source: Internet
Author: User

When using hibernate SQL queries in multi-table queries, the object[] array is generally returned, or you can use the

Session.createsqlquery (SQL). Setresulttransformer (Transformers.alias_to_entity_map);

To map for processing, you can turn query results into entity classes in the following ways:

1,sql statement String sql= "select S.id as id,s.classname as classname from grade S,student St where S.id=st.classid"

Use the following statement

Session.createsqlquery (SQL). Setresulttransformer (Transformers.aliastobean (Grade.class)) You can convert the fields queried in SQL into class, but you must specify an alias for each field, which is the corresponding attribute in class class, but be aware that each column must be addscalar ("column name"). If you do not set the Addscalar method, you may be able to report an exception for a transformation error. If the query results include fields from multiple tables and cannot be accepted for an entity class, create a new entity class that corresponds to the query result, or use the map result set directly

2,sql statement String sql= "SELECT {s.*} from grade S,student St where S.id=st.classid"

SQL that is queried for all of these fields can be converted to entity classes using addentity

SQLQuery query=session.createsqlquery (SQL);

Query.addentity ("s", Grade.class);

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.

This allows you to assign the values of all the fields in the class table to the class Java training organization ranking , and must be all attributes

3,sql statement String sql= "SELECT {s.*},{st.*} from grade S,student St where S.id=st.classid"

SQLQuery query=session.createsqlquery (SQL);

Query.addentity ("s", Grade.class);

Query.addentity ("St", Student.class);

Using such a query to get a object[] array, object[0] is the class entity class, Object[1] is the student entity class

You can use the Addscalar (String arg,type type) method to define the type of field to return, such as

S.createsqlquery (SHUIQINGHQL). Addscalar ("Stcd", hibernate.string). Addscalar ("STNM")

Hibernate SQL queries converted to entity classes

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.