Converting hibernate SQL queries to entity classes
When you use hibernate SQL to query multiple tables, the object [] array is generally returned, or session can be used. createSQLQuery (SQL ). setResultTransformer (Transformers. ALIAS_TO_ENTITY_MAP); to convert to map for processing. You can convert the query results to object 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 "uses the following statement session. createSQLQuery (SQL ). setResultTransformer (Transformers. aliasToBean (Grade. class) You can convert the fields queried in SQL into the class entity class, The alias must be specified for each field. The alias is the corresponding attribute in the class, but note that addScalar ("column name") is required for each column "), if you do not set the addScalar method, the system may report an error in transformation. if the query results contain fields in multiple tables and cannot be accepted by one object class, you need to create an object class corresponding to the query results, or directly use map result set 2, SQL statement String SQL = "select {s. *} from grade s, student st where s. id = st. classid "the SQL statements used to query all such fields can be converted into an object class SQLQuery query = session BY USING addEntity. createSQLQuery (SQL); query. addEntity ("s", Grade. class); An SQL query statement with a placeholder that allows Hibernate to use the field alias. queries the returned object and its SQL table alias. a The ddEntity () method associates the alias of the SQL table with the entity class and determines the form of the query result set. In this way, the values of all fields in the class table can be assigned to the Class Object Class, which must be all attributes 3. the 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); the query result is an object [] array, object [0] is the class entity class, object [1] is the Student Entity class, you can use addScalar (String arg, type type) the method defines the Type of the field to be returned, such as s. createSQLQuery (shuiQingHQL ). addScalar ("STCD", Hibernate. STRING ). addScalar ("STNM ")