Hibernate implements addition, deletion, modification, and query, while hibernate implements addition and deletion.

Source: Internet
Author: User

Hibernate implements addition, deletion, modification, and query, while hibernate implements addition and deletion.

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 <Login> queryAll () {Session session = HibernateSessionFactory. getSession (); String hql = "FROM Login"; List <Login> 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 {Hibe RnateSessionFactory. closeSession ();} return list;} // public List for fuzzy query <Login> queryLike (String name) {Session session = HibernateSessionFactory. getSession (); String hql = "from Login as l where l. username like? "; Query query = session. createQuery (hql); query. setString (0, "%" + name + "%"); List <Login> 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 <Login> list = que Ry. list (); int totalCount = list. size (); int pageSize = (totalCount % maxResult = 0 )? (TotalCount/maxResult) :( totalCount/maxResult + 1); query. setFirstResult (firstResult); query. setMaxResults (maxResult); List <Login> listSize = query. list (); qr = new QueryResult (pageSize, listSize); tran. commit ();} catch (Exception e) {tran. rollback ();} finally {HibernateSessionFactory. closeSession () ;}return qr ;}}

 


How does hibernate add, delete, modify, and query functions?

Go to Baidu. Here, too few words are written.

The basic method is the implementation of the Session factory.
Configuration cfg = new Configuration (). configure ();
SessionFactory sf = cfg. buildSessionFactory ();
Session session = sf. openSession ();
Transaction ts = ses. beginTransaction ();

Add session. save (Object Name );

Delete session. del ();

Change session. updete ();

Find session. find ();

Hibernate addition, deletion, modification, and query

Handsome guy, I added you.

Do not delete GM posts. I spoke with the landlord on qq.

Related Article

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.