Converting hibernate SQL queries to entity classes

Source: Internet
Author: User

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 ")

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.