Today, we see a piece of code using hibernate. In order to count the data of each department, We have to first find all the qualified data, then traverse the set to retrieve all the departments in the data, then, traverse the set again and compare it with the department name just retrieved. Add 1 to the counter with the same name, and then you can get the statistics of each department.
I am not bothered to see it here. If you use an SQL statement, simply write "select Department name, count (*) from table where condition. When using hibernate, hql statements are generally used to writing directly from, and return a set of classes. In fact, when using hibernate, you can also write from select, "select Department name, count (*) from class where condition", the result is an array of objects returned for the attribute. Here, an array of department names and quantity is returned.
String hql = "Select name, count (*) from Dept ";
List list = gethibernatetemplate (). Find (hql );
Here, a list is returned. At this step, I forgot how to retrieve the data in the list. I checked it online and changed the object in the list.
Object [] OBJ = (object []) List;
Then string name = OBJ [0];
Long Count = OBJ [1];
Put the data into a hashmap return page, so that the data of each department can be obtained.
The object [] OBJ = (object []) list; is stuck. According to the inertial thinking of hibernate, define a class to retrieve the data in the list, so I defined
Public class test {
Private string name;
Private int count;
Public test (){}
Public String [] getname (){
Return name;
}
Public void setname (string [] Name ){
This. Name = Name;
}
Public int getcount (){
Return count;
}
Public void setcount (INT count ){
This. Count = count;
};
}
I couldn't retrieve the list. debug looked at the list value,
We can see that count is of the long type and thought it was a private int count error.
Therefore, it is changed
Public class test {
Private string [] Name;
Private long count;
Public test (){}
Public String [] getname (){
Return name;
}
Public void setname (string [] Name ){
This. Name = Name;
}
Public long getcount (){
Return count;
}
Public void setcount (Long Count ){
This. Count = count;
};
}
A type conversion error is reported. A friend reminds me that an array is returned. Use object [] to retrieve the array.
I forgot to write it. I mentioned on the Internet that the list of returned object arrays cannot be traversed using iterator, but I tried it.
While (it. hasnext ()){
Object [] B = (object []) it. Next ();
String name = (string) B [0];
Long Count = (long) B [1];
System. Out. println (name + "" + count );
}
This code can still be executed. I don't know what he is talking about?
I have been in the company for more than eight months. I have basically not written code, and I have done a lot of chores. Instead, I have regressed in technology and forgot all the basic core Java knowledge, you have to start over and stick to the foundation before you can go up.