hibernate--simple additions and deletions to change the search

Source: Internet
Author: User
Tags commit config rollback

Additions and deletions are relatively simple, check a little more complex. There are two ways to query, one is through the HQL statement and the query interface to achieve, one is the conditional query, through the Ctiteria interface to achieve, the following examples illustrate:

Userdao Interface: Package

Com.suo.hibernate.dao;

Import java.util.List;

Import Com.suo.domain.User;

Public interface Userdao {public
	void Saveuser (user user);
	Public User Finduserbyid (int id);
	Public list<user> finduserbyname (String name);
	public void Removeuser (user user);
	public void UpdateUser (user user);
 Get session Tool class: Package com.suo.hibernate.util;

Import java.io.Serializable;
Import org.hibernate.HibernateException;
Import org.hibernate.Session;
Import Org.hibernate.SessionFactory;
Import org.hibernate.Transaction;

Import org.hibernate.cfg.Configuration;

Import Com.suo.domain.User;
	
	Public final class Hibernateutil {private static sessionfactory sessionfactory;
	/** * Private constructor, which prevents the instantiation of this class */private Hibernateutil () {}/** * because it takes time to read the configuration file and the mapping file, so this block of code is defined as static, so it can be loaded only once */
		static{Configuration config=new configuration (); Config.configure ();//Read the configuration file, the default is read Hibernate.cfg.xml, if the file name is not this, can also be specified as a parameter sessionfactory=
	Config.buildsessionfactory ();
	} public static Sessionfactory Getsessionfactory () {return sessionfactory;
	} public static Session getsession () {return sessionfactory.opensession (); }
}
Implementation class for interface: Userdaoimpl package com.suo.hibernate.impl;

Import java.util.List;
Import org.hibernate.HibernateException;
Import Org.hibernate.Query;
Import org.hibernate.Session;

Import org.hibernate.Transaction;
Import Com.suo.domain.User;
Import Com.suo.hibernate.dao.UserDao;

Import Com.suo.hibernate.util.HibernateUtil;
		Public final class Userdaoimpl implements Userdao {public void Saveuser (user user) {Session session=null;
		Transaction Transaction=null; try{session=hibernateutil.getsession ();//Get a connection transaction=session.begintransaction ();//Open a transaction session.save (use
			R);
		Transaction.commit ();
			}catch (hibernateexception e) {if (transaction!=null) {transaction.rollback ();
		} e.printstacktrace ();
			}finally{if (session!=null) {session.close (); }}}/** * Query operation via the GET method of Session * @param ID is queried by primary key * @return return query to object if present, otherwise return empty */public User finduserbyi
		d (int id) {Session session=null;
		try{session=hibernateutil.getsession ();	
			User user= (user) Session.get (user.class, id);
			
		return user;
			}catch (hibernateexception e) {e.printstacktrace ();
		return null;
			}finally{if (session!=null) {session.close ();
		}}}/** * Query operation via HQL statement * @param name is queried by the name key */public list<user> finduserbyname (String name) {
		Session Session=null;
			
			try{session=hibernateutil.getsession ();
			String hql= "from user as user where user.name=?";
			Query query=session.createquery (HQL);
			Query.setstring (0, name);
			
			List<user> list=query.list ();
		return list;
			}catch (hibernateexception e) {e.printstacktrace ();
		return null;
			}finally{if (session!=null) {session.close ();
		}}}/** * Delete an object */public void Removeuser (user user) {Session session=null;
		Transaction Transaction=null; try{session=hibernateutil.getsession ();//Get a connection transaction=session.begintransaction ();//Open a Transaction Session.delete (U
	SER);//This is not quite clear here, the user is in the off-pipe state, in the end how to delete it.		Transaction.commit ();
			}catch (hibernateexception e) {if (transaction!=null) {transaction.rollback ();
		} e.printstacktrace ();
			}finally{if (session!=null) {session.close ();
		}}}/** * Update an object */public void UpdateUser (user user) {Session session=null;
		Transaction Transaction=null; try{session=hibernateutil.getsession ();//Get a connection transaction=session.begintransaction ();//Open a Transaction session.update (U
			SER);
		Transaction.commit ();
			}catch (hibernateexception e) {if (transaction!=null) {transaction.rollback ();
		} e.printstacktrace ();
			}finally{if (session!=null) {session.close ();
 }
		}
	}
	
}

It is important to note that deleting an object with the delete in session and updating an object with update are all through the ID to find the object, the SQL statement executed by it can be known, so, when you delete an object, you first need to new a user, then specify the ID, will be correctly deleted and updated, in addition, the SQL statement for the update operation is to update all attributes other than the ID, so for updates, the individual property values are set, except to indicate the ID, otherwise they will all be reset to the default values.


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.