Hibernate of additions and deletions (based on getcurrentsession) _hibernate

Source: Internet
Author: User

Bloggers in learning to hibernate the original SQL additions and deletions have a bit of doubt, especially when the getcurrentsession, to query a single data, modify the data is very confused, today found an article, specially reproduced to the blog, and you share!


public class Userdaoimpl implements Userdao {@Autowired private sessionfactory sessionfactory; Log on to public User selectuser (user user) throws Exception {query query = sessionfactory.getcurrentsession (). CreateQuery ( "From User u where u.name=?"
		and u.password=? ");
		Query.setstring (0, User.getname ());
		Query.setstring (1, User.getpassword ());
		List List = Query.list (); if (list==null| |
		List.size () ==0) {throw new RuntimeException ("Query failed");
	Return List.get (0); //Query All public List getallusers () throws Exception {query query = sessionfactory.getcurrentsession (). CreateQuery ("
		From User ");
		List List = Query.list ();
	return list; ///Single query public User getuser (Integer id) throws Exception {return (User) sessionfactory.getcurrentsession (). Createq
	Uery ("From User u where u.id =" +id). Uniqueresult ();
		//Add user public void AddUser (username) throws Exception {System.out.println ("11111111111111111" +user.getname ());
Sessionfactory.getcurrentsession (). Save (user);	//Remove user public void Deluser (Integer id) throws Exception {sessionfactory.getcurrentsession (). CreateQuery ("delete	   	
		
	User u where u.id= "+id). Executeupdate (); 
		 }//Modify user public void UpdateUser (username user) throws Exception {Session session = Sessionfactory.getcurrentsession ();
		 Session.begintransaction (); String hql = ("Update User u set u.name =?, U.password =?, U.logindate =?")  
		 where u.id =? ");
		 Query query = session.createquery (HQL);
		 Query.setparameter (0, User.getname ());
		 Query.setparameter (1, User.getpassword ());
		 Query.setparameter (2, User.getlogindate ());
		 Query.setparameter (3, User.getid ());
		 Query.executeupdate (); 			
	Session.gettransaction (). commit (); }
}


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.