Standard example of adding, deleting, modifying, and querying hibernate

Source: Internet
Author: User

An example of adding, deleting, modifying, and querying a small file written using the hibernate framework is as follows: it is a standard writing format; [java] package www.csdn.net. bookhome. daoimpl; import java. util. list; import org. hibernate. session; import org. hibernate. transaction; import www.csdn.net. bookhome. dao. adminDao; import www.csdn.net. bookhome. dao. baseHibernateDao; import www.csdn.net. bookhome. domain. admin; import www.csdn.net. bookhome. utils. hibernateSessionFactory; public class AdminDaoImpl extends BaseHibernateDao implements AdminDao {public void deleteObject (Admin entity) {Transaction tx = null; try {Session session = getSession (); tx = Session. beginTransaction (); session. delete (entity); tx. commit ();} catch (Exception e) {tx. rollback (); throw new RuntimeException ("delete all errors" + e);} finally {HibernateSessionFactory. closeSession () ;}} public void deleteObjectById (Integer id) {Transaction tx = null; try {Session session = getSession (); tx = session. beginTransaction (); session. save (id); tx. commit ();} catch (Exception e) {tx. rollback (); throw new RuntimeException ("Error Based on id" + e);} finally {HibernateSessionFactory. closeSession () ;}} public List getAllObjects (Class entityClass) {try {return getSession (). createQuery ("from Admin "). list ();} catch (Exception e) {throw new RuntimeException ("Search error" + e);} finally {HibernateSessionFactory. closeSession () ;}} public Admin getObjectById (Class className, Integer id) {try {return (Admin) getSession (). get (className, id);} catch (Exception e) {throw new RuntimeException ("error found by id" + e);} finally {HibernateSessionFactory. closeSession () ;}} public List getObjects (Class clazz, int from, int size, String orderName) {try {return getSession (). createQuery ("from Admin "). setFirstResult (from-1) * size ). setMaxResults (size ). list ();} catch (Exception e) {throw new RuntimeException ("Paging query error" + e);} finally {HibernateSessionFactory. closeSession () ;}} public Admin loadObjectById (Class className, Integer id) {try {return (Admin) getSession (). load (className, id);} catch (Exception e) {throw new RuntimeException ("load query error" + e);} finally {HibernateSessionFactory. closeSession () ;}} public void saveObject (Admin entity) {Transaction tx = null; try {Session session = getSession (); tx = session. beginTransaction (); session. save (entity); tx. commit ();} catch (Exception e) {tx. rollback (); throw new RuntimeException ("Save error" + e);} finally {HibernateSessionFactory. closeSession () ;}} public void updateObject (Admin entity) {Transaction tx = null; try {Session session = getSession (); tx = session. beginTransaction (); session. update (entity); tx. commit ();} catch (Exception e) {tx. rollback (); throw new RuntimeException ("Update error" + e);} finally {HibernateSessionFactory. closeSession () ;}} public Admin getAllObjects (String name) {try {return (Admin) getSession (). createQuery ("from Admin a where. adminName =: name "). setParameter ("name", name ). uniqueResult ();} catch (Exception e) {throw new RuntimeException ("error found by user query" + e);} finally {HibernateSessionFactory. closeSession () ;}} public Admin login (Admin entity) {try {return (Admin) getSession (). createQuery ("from Admin a where. adminName =: name and. adminPassword =: pass "). setString ("name", entity. getAdminName ()). setString ("pass", entity. getAdminPassword ()). uniqueResult ();} catch (Exception e) {throw new RuntimeException ("Logon error" + e);} finally {HibernateSessionFactory. closeSession ();}}}

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.