Four typical examples of hibernate (add and revise)
Inquire
Sessionsession=hibernatesessionfactory.getsession ();
Session.begintransaction ();
Query q=session.createquery ("from Infowhere id=:id");
Q.setinteger ("id", 1);
List l=q.list ();
Info i= (info) l.get (0);
System.out.print (I.getname () +i.getpwd ());
SYSTEM.OUT.PRINTLN ("Query Success");
Delete
Sessionsession=hibernatesessionfactory.getsession ();
Transaction t=session.gettransaction ();
T.begin ();
Query q=session.createquery ("from Infowhere name=:name");
Q.setstring ("name", "KK");
List l=q.list ();
Info info= (info) l.get (0);
Session.delete (info);
T.commit ();
System.out.print ("delete succeeded");
Add to
Session session=hibernatesessionfactory.getsession ();
Transaction t=session.gettransaction ();
T.begin ();
Info info=new info ();
Info.setid (3);
Info.setname ("k9999");
Info.setpwd ("333");
Session.save (info);
T.commit ();
System.out.print ("add success");
Modify
Sessionsession=hibernatesessionfactory.getsession ();
Transaction t=session.gettransaction ();
T.begin ();
Info info=new info ();
Info.setid (2);
Info.setname ("Dafei");
Info.setpwd ("Shazi");
Session.update (info);
T.commit ();
System.out.print ("modified successfully");
Four typical examples of hibernate (add and revise)