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 report a transition error exception . If the results of a query include fields from multiple tables and cannot be accepted for an entity class, you need to 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 entity class, 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")
This article is from the "BREEZEWINDLW" blog, make sure to keep this source http://breezewindlw.blog.51cto.com/6504579/1714010
Hibernate SQL queries converted to entity classes