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 formerHibernate-generated bean for object Mount list return
The latter is stored as an array of objects
So using createsqlquery sometimes also wants toHibernate-generated beans are not convenient for loading a list of objects.
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 ();
CreateQuery and Createsqlquery