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.