Comparison of Addentity Setresulttransformer in Hibernate

Source: Internet
Author: User

hibernate does not automatically wrap the results into entities when querying queries using native SQL statements.
so a series of methods such as Addentity (class class) should be called manually.
1, session.createsqlquery (SQL). Addentity (class Class);
Note hibernate3.0.5 does not support the Addentity method of a single parameter
2, Session.createsqlquery (SQL). Setresulttransformer (Transformers.aliastobean (clazz.class))
or Setresulttransformer (new Aliastobeanresulttransformer (Clazz.class))
returns entities that are not managed by hibernate.
The above query will return a list of clazz, which will be instantiated and inject the value of the list item into the corresponding attribute or field.
http://bbs.xml.org.cn/blog/more.asp?name=lhwork&id=15351
It must be noted, however, that each column must be addscalar ("column name")
So, what is the difference between Setresulttransformer and addentity?
The former supports checking arbitrary columns, which must be in the form of select * from users or select {a.*},{b.*} from a, a where .....

Querying managed Objects

String sql = "Select c.* from Clazz C";

list<clazz> Clazzs = (list<clazz>) session.createsqlquery (SQL). Addentity (Clazz). List ();

String sql = "Select c.id from Clazz C";

list<clazz> Clazzs = (list<clazz>) session.createsqlquery (SQL). Addscalar ("id", Hibernate.long). Addscalar ("name", hibernate.string)
. Setresulttransformer (Transformers.aliastobean (Clazz.class)). List ();
Special attention:
1. If you do not set the Addscalar method, you may report a transition error exception.
2. Managed queries can use projections to query only the specified column properties

Summarize:

Hibernate entity class with addentity

Setresulttransformer for entity classes not managed by hibernate

Comparison of Addentity Setresulttransformer in Hibernate

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.