A summary of the modification of hibernate additions and deletions

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.