Comparison of addentity setresulttransformer in hibernate

Source: Internet
Author: User

If native SQL statements are used for query queries, Hibernate does not automatically wrap the results into entities. Therefore, you must manually call a series of methods such as addentity (Class class.

For example, session. createsqlquery (SQL). addentity (Class class); note that hibernate3.0.5 is not supported. The addentity method of a single parameter

In addition, hibernate3.2 can use resulttransformer for native SQL queries. This will return entities not managed by hibernate.
Session. createsqlquery ("Select name, birthdate from cats ")
. Setresulttransformer (transformers. aliastobean (catdto. Class ))

Or setresulttransformer (New aliastobeanresulttransformer (catdto. Class ))
The above query will return the catdto list, which will be instantiated and the value of name and birthday will be injected into the corresponding attribute or field.
Http://bbs.xml.org.cn/blog/more.asp? Name = lhwork & id = 15351

Note that addscalar ("column name") is required for each column ")

In this case,

So what is the difference between setresulttransformer and addentity? One difference is that the former supports querying arbitrary columns, and the latter must be in the form of select * from users or select

{A. *}, {B. *} from a, B where .....

Query Managed Objects

View plaincopy to clipboardprint?
String SQL = "select C. ID, C. name from clazz C, student s where S. class_id = C. ID ";

List <clazz> clazzs = (list <clazz>) Session. createsqlquery (SQL)

. Addscalar ("ID", hibernate. Long)

. Addscalar ("name", hibernate. String)

. Setresulttransformer (transformers. aliastobean (clazz. Class). List ();
String SQL = "select C. ID, C. name from clazz C, student s where S. class_id = C. ID ";
List <clazz> clazzs = (list <clazz>) Session. createsqlquery (SQL)
. Addscalar ("ID", hibernate. Long)
. Addscalar ("name", hibernate. String)
. Setresulttransformer (transformers. aliastobean (clazz. Class). List ();

Note:

1. If the addscalar method is not set, an error in transformation may be reported.

2. You can use projection to query only the specified column attributes.

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.