The original site of this article: Http://stta04.javaeye.com/blog/377633hibernate CreateQuery and Createsqlquery
Last night to help colleagues see the code to 2 o'clock in the morning, this morning 6 o'clock woke up to find that he sent a message saying null pointer error, really can't sleep, up his own test, console also really reported:
2009-4-25 8:12:34 Org.apache.catalina.core.ApplicationContext Log
Info: java.lang.ClassCastException: [Ljava.lang.Object; cannot is cast to Com.miracle.dm.doc.catalog.model.DocCatalogInfo
The original query statement:
String sql = "Select a.* from Tb_doc_catalog a where a.cat_code like '" +catcode+ "% '";
Session session = This.getsession ();
try {
List catnamelist = session.createsqlquery (SQL). List ();
return catnamelist;
} finally {
Releasesession (session); Release session
}
Analysis: Fields that were originally queried are not automatically converted to bean objects.
Solution one (using HQL query):
String sql = "Select a from Doccataloginfo a where a.catcode like '" +catcode+ "% '";
List catnamelist =gethibernatetemplate (). find (SQL);
return catnamelist;
OK, test to find no problem, it seems still because of the original SQL query reason, search online: Createsqlquery returned to the object, saw an article only to realize:
Solution two (with native SQL query):
String sql = "Select a.* from Tb_doc_catalog a where a.cat_code like '" +catcode+ "% '";
Session session = This.getsession ();
try {
List catnamelist = session.createsqlquery (sql). Addentity (Doccataloginfo.class). List ();
return catnamelist;
} finally {
Releasesession (session); Release session
}
It's OK again.
This article is also posted:
The difference between CreateQuery and Createsqlquery in Hibernate is:
The former uses the HQL statement to query, the latter can use the SQL statement query
The former takes the bean generated by hibernate as the object to load the list back
The latter is stored as an array of objects
So it's not very convenient to use createsqlquery and sometimes want to load the list back with hibernate-generated beans.
Suddenly discovered that Createsqlquery had a way to convert objects directly
Query query = session.createsqlquery (SQL). addentity (Xxxxxxx.class);
XXXXXXX represents the object of the bean generated by hibernate, which is the bean that the data table maps out.
Hehe after more attention, or from time to time to see the use of various methods hibernate object.
There is another minor detail that should be noted:
For example, there is such a PO
PO:User.class
Properties:userid,username
Ddl:create table Tuser (userid varchar), username varchar (20));
When executing:
Session.createquery ("From User U"). The SQL generated when list ():
Select Userid,username from Tuser;
When executing:
SQL generated by Session.createquery ("from User U"). Iterator ():
Select UserID from Tuser;
You can see that list () reads data from the database at once and fills it directly into the list
Iterator () reads the primary key of the data from the database and adds execution when the iterator is looped:
Select Userid,username from user where userid=?; read out the data.
Different methods are used in different application areas and should be noted in hibernate applications.
The second explanation
Today, I want to make a paging DAO, using Hibernate to fetch 5 data from the database's question table, which takes a piece of data out of the database each time, rather than fetching all the data at once.
In the beginning, my DAO was written like this:
Public List findallquestion (int num,int pagesize) {
int num2= (NUM-1);
Session session=getsession ();
String sql= "SELECT * from Question limit:num1,:size";
Query query = session.createsqlquery (SQL);
Query.setparameter ("Num1", num2);
Query.setparameter ("size", pagesize);
List list=query.list ();
Question Question=null;
for (int i=0;i<list.size (); i++)
{
Question = (question) list.get (i);
System.out.print ("ID" +question.getid ());
System.out.println ("TITLE" +question.gettitle ());
}
return list;
}
Strange things just happen,
First, an error occurred:
Java.lang.ClassCastException: [Ljava.lang.Object; cannot is cast to com.njcit.bbs.Question
Second, there is no value in the question object
This I am strange, I use List.size () found that the length is really 5, and I need the length is consistent, why the error, and can not be taken out?
The problem should be Question = (Question) list.get (i); list is not a store of records, why not turn into the bean I need it?
The internet to find a lot of information, and finally found here the thing to want: http://helloandy2009.javaeye.com/blog/614369
We generally use hibernate to write additions and deletions when the change, there are 2 of ways, one is HQL, one is SQL
Instance:
The statement I want to execute is: SELECT * from Question
That's what HQL wrote:
Session session = GetSession ();
String hql= "from Question";
List List = (Question) session.createquery (HQL). List ();
System.out.println (List.gettitle ());
Test Normal
SQL notation:
Session session=getsession ();
String sql= "SELECT * from Question"
List list= (Question) session.createsqlquery (SQL). List ();//A type conversion error occurs, which is the error at the beginning of the article
Reason:
In HQL
String hql= "from Question";
List List = (Question) session.createquery (HQL). List ();
Based on your HQL statement, the return object of the Session.createquery (HQL) list () is automatically loaded with the bean generated by Hibernate as the object in List return
In SQL
String sql= "SELECT * from Question"
List list= (Question) session.createsqlquery (SQL). List ();
is stored in an object array to return
Bottom line: HQL: The returned list is Bean SQL: The returned object is an array, and when the array is converted to a question object, it will of course be error-filled.
Workaround:
The first kind: directly and honestly use HQL to write it = = parameterization on Baidu "Hibernate parameter binding" on the line
The second type: Using native SQL, call one of the methods Addentity ()
String sql= "SELECT * from Question";
Query query = session.createsqlquery (SQL). addentity (Xxxxxxx.class);
List List = (Question) query;
So the list is also installed is question this one bean object, the problem is still do not know how to parameterize = =!
Find a way:
The paging statement in Hibernate can be written like this
Session = Hibernateutils.getsession ();
Session.begintransaction ();
Query query = Session.createquery ("from User");
Query.setfirstresult (0);//start with the first record
Query.setmaxresults (4);//Remove four records
List userlist = Query.list ();
The difference between Createsqlquery and CreateQuery