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