Hibernate single-table type data processing generic class

Source: Internet
Author: User

Package Chengfei.hibernate.Dao;

Import java.util.List;
Import org.hibernate.HibernateException;
Import org.hibernate.Session;
Import org.hibernate.Transaction;
Import Chengfei.hibernate.tool.HibernateUtils;

/**hibernate Database Operations Generic classes
* @author Administrator
* @param <T>
*/
public class Service<t> {

Private class<t> C;

Public Service (class<t> c) {
THIS.C = C;
}

/** Save
* @param t
*/
public void Save (T t) {
Session session = Hibernateutils.opensession ();
Transaction tx = NULL;
try {
tx = Session.begintransaction ();
Session.save (t);
Tx.commit ();

} catch (Hibernateexception e) {
Tx.rollback ();
Throw e;
} finally {
Session.close ();
}
}

/** Delete
* @param ID
*/
public void Delete (Integer id) {
Session session = Hibernateutils.opensession ();
Transaction tx = NULL;
try {
tx = Session.begintransaction ();
T t = This.getbyid (ID);
Session.delete (t);
Tx.commit ();
} catch (Hibernateexception e) {
Tx.rollback ();
Throw e;
} finally {
Session.close ();
}

}

/** Update
* @param t
*/
public void update (T t) {
Session session = Hibernateutils.opensession ();
Transaction tx = NULL;
try {
tx = Session.begintransaction ();
Session.update (t);
Tx.commit ();
} catch (Hibernateexception e) {
Tx.rollback ();
Throw e;
} finally {
Session.close ();
}

}

/** Get Unique objects
* @param ID
* @return
*/
@SuppressWarnings ("Unchecked")
Public T getById (Integer ID) {
Session session = Hibernateutils.opensession ();
Transaction tx = NULL;
try {
tx = Session.begintransaction ();
T t = (t) session.get (c, id);
Tx.commit ();
return t;
} catch (Hibernateexception e) {
Tx.rollback ();
Throw e;
} finally {
Session.close ();
}
}

/** Get all
* @return
*/
@SuppressWarnings ("Unchecked")
Public list<t> FindAll () {
Session session = Hibernateutils.opensession ();
Transaction tx = NULL;
try {
tx = Session.begintransaction ();
list<t> list = Session.createquery ("from" + C.getsimplename ())
. List ();
Tx.commit ();
return list;
} catch (Hibernateexception e) {
Tx.rollback ();
Throw e;
} finally {
Session.close ();
}
}

/** to get paged data
* @param firstresult
* @param maxresult
* @return
*/
@SuppressWarnings ({"Unchecked", "Rawtypes"})
Public queryresult FindAll (int firstresult, int maxresult) {
Session session = Hibernateutils.opensession ();
Transaction tx = NULL;
try {
tx = Session.begintransaction ();
list<t> list = Session.createquery (//
"From" + C.getsimplename ())//
. Setfirstresult (Firstresult)//
. Setmaxresults (Maxresult). List ();
Tx.commit ();
Long Count = (long) session.createquery (
"SELECT COUNT (*) from" + C.getsimplename ())//
. Uniqueresult ();
return new QueryResult (list, count.intvalue ());
} catch (Hibernateexception e) {
Tx.rollback ();
Throw e;
} finally {
Session.close ();
}

}

}

Hibernate single-table type data processing generic class

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.