Hibernate implements the most basic addition, deletion, modification, and query

Source: Internet
Author: User
Hibernate provides rich APIs for database operations. Session is a very important class for database operations. It provides basic operations for adding, deleting, modifying, and querying databases. The following examples show how to add, delete, and update databases in the instance, query all databases by ID, query by PAGE, and fuzzy query packaged.

Hibernate provides rich APIs for database operations. Session is a very important class for database operations. It provides basic operations for adding, deleting, modifying, and querying databases. The following examples show how to add, delete, and update databases in the instance, query all databases by ID, query by PAGE, and fuzzy query of package d.

Hibernate provides rich APIs for database operations.

Session is a very important class for database operations. It provides basic operations for adding, deleting, modifying, and querying databases.

The following examples show how to add, delete, and update databases in the instance, query all databases by ID, query by PAGE, and fuzzy query.

Package dao; import java. util. list; import org. hibernate. hibernateException; import org. hibernate. query; import org. hibernate. session; import org. hibernate. transaction; import sessionfactory. hibernateSessionFactory; import entity. login; public class BaseDao {// save user public void save (Login login) {Session session = HibernateSessionFactory. getSession (); Transaction tran = null; try {tran = session. beginTransact Ion (); session. save (login); tran. commit (); System. out. println ("added successfully! ");} Catch (HibernateException e) {tran. rollback (); System. out. println (" insertion failed! ");} Finally {HibernateSessionFactory. closeSession () ;}}// delete user public void delete (Login login) {Session session = HibernateSessionFactory. getSession (); Transaction tran = null; try {tran = session. beginTransaction (); session. delete (login); System. out. println ("deleted successfully"); tran. commit ();} catch (Exception e) {System. out. println ("failed to delete"); tran. rollback ();} finally {HibernateSessionFactory. closeSession () ;}// updates the user Information public void update (Login login) {Session session = HibernateSessionFactory. getSession (); Transaction tran = null; try {tran = session. beginTransaction (); session. update (login); tran. commit (); System. out. println ("Update successful! ");} Catch (Exception e) {tran. rollback (); System. out. println (" update failed! ");} Finally {HibernateSessionFactory. closeSession () ;}}// query public Login queryById (int id) {Login login = null; Session session Session = HibernateSessionFactory by user id. getSession (); Transaction tran = null; try {tran = session. beginTransaction (); login = (Login) session. get (Login. class, id); tran. commit ();} catch (Exception e) {tran. rollback (); System. out. println ("user not found! ");} Finally {HibernateSessionFactory. closeSession ();} return login;} // query all public lists
 
  
QueryAll () {Session session = HibernateSessionFactory. getSession (); String hql = "FROM Login"; List
  
   
List = null; Transaction tran = null; try {tran = session. beginTransaction (); Query query = session. createQuery (hql); list = query. list (); tran. commit ();} catch (Exception e) {tran. rollback ();} finally {HibernateSessionFactory. closeSession ();} return list;} // fuzzy query public List
   
    
QueryLike (String name) {Session session Session = HibernateSessionFactory. getSession (); String hql = "from Login as l where l. username like? "; Query query = session. createQuery (hql); query. setString (0," % "+ name +" % "); List
    
     
List = query. list (); return list;} // public QueryResult queryAll (int firstResult, int maxResult) {QueryResult qr = null; Session session = HibernateSessionFactory. getSession (); Transaction tran = null; try {tran = session. beginTransaction (); Query query = session. createQuery ("FROM Login"); List
     
      
List = query. list (); int totalCount = list. size (); int pageSize = (totalCount % maxResult = 0 )? (TotalCount/maxResult) :( totalCount/maxResult + 1); query. setFirstResult (firstResult); query. setMaxResults (maxResult); List
      
        ListSize = query. list (); qr = new QueryResult (pageSize, listSize); tran. commit ();} catch (Exception e) {tran. rollback ();} finally {HibernateSessionFactory. closeSession () ;}return qr ;}}
      
     
    
   
  
 

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.