A summary of the modification of hibernate additions and deletions
(1)Hql Update
A. note that this is a partial update . If the update session.update (JS) directly; all fields will be updated by default. When you set the updated character, it takesthe form of a subscript (0,1,2,3).
@Override
Public void Upadtejs (BJs js) {
BJs is a class
String hql = "Update BJs JS set js.jsxm=?,js.jsztbm=?", js.xlbm=?,js.xwbm=?,js.zcbm=?,js.bmbm=? where js.jsbh =? ";
Query Q = this. getsession (). CreateQuery (hql);
Q.setstring (0, JS.GETJSBH ());
Q.setString (0, js. GETJSXM ());
Q.setString (1, js. GETJSZTBM ());
Q.setString (2, js. GETXLBM ());
Q.setString (3, js. GETXWBM ());
Q.setString (4, js. GETZCBM ());
Q.setString (5, js. GETBMBM ());
Q.setString (6, js. GETJSBH ());
Q.executeupdate ();
}
B more flexible named parameter forms
String hql = "Update BJs JS set js.jsxm=:jsxm where js.jsbh =: jxbh ";
Query query=s.createquery (HQL);
query.setstring ("jsxm ", tjsbh);
Notice that the red part above is the character to be replaced.
(2)HQL Delete
Delete directly with SQL
Public boolean deleteteammember (int teamid, String teachernum) {
String SQL = "DELETE from Tblteamtoteacher where (teamid=?) and (thnum=?) ";
SQLQuery Query = this. getsession (). createsqlquery (sql);
query. Setparameter (0, teamid);
query. Setparameter (1, teachernum);
int num= query. executeupdate ();
if (num>0)
return true;
Else
return false;
}
Use hql to delete
Public void Delete (int ID) {
String hql="Delete person as P where p.id=?";
Query query=session.createquery (hql);
query. Setinteger (0,ID);
query. executeupdate ();
Session.begintransaction (). commit ();
}
(3) Enquiry
Static void Query (String name) {
Session s=null;
Try {
S=hibernateutil.getsession ();
//from is followed by an object, not a table name
String hql="from admin as admin where Admin.aname=:name"; // use named parameters, recommended, easy to read.
Query query=s.createquery (HQL);
Query.setstring ("name", name);
List<admin> list=query.list ();
for (Admin admin:list) {
System.out.println (Admin.getaname ());
}
}finally{
if (s!=null)
S.close ();
}
}
A summary of the modification of hibernate additions and deletions