Abstract class implementation of JPA persistence layer curd operations

Source: Internet
Author: User
Tags abstract commit object object rollback

JPA has provided us with a rich API interface, the following for its various APIs encapsulated into an abstract class, our business logic interface implementation as long as the inheritance of this abstract class, you can use simple code to achieve a variety of operations.

Add action: protected void Persist (Object object) throws Persistenceexception {
Persistenceexception ex = null;
Entitymanager em = Getentitymanager ();
Em.gettransaction (). Begin ();

try {
Persist (em, object);
Em.gettransaction (). commit ();
} catch (Persistenceexception e) {
ex = e;
try {
Em.gettransaction (). rollback ();
} catch (Exception innerexception) {
}
} catch (Exception e) {
ex = new Persistenceexception (e);
try {
Em.gettransaction (). rollback ();
} catch (Exception innerexception) {
}
} finally {
Em.close ();
}

if (ex! = null) {
Throw ex;
}
}

Update operation: protected <T> t Merge (T object) throws Persistenceexception {
T ro = null;
Persistenceexception ex = null;
Entitymanager em = Getentitymanager ();
Em.gettransaction (). Begin ();
try {
Ro = Merge (em, object);
Em.gettransaction (). commit ();
} catch (Persistenceexception e) {
ex = e;
try {
Em.gettransaction (). rollback ();
} catch (Exception innerexception) {
}
} catch (Exception e) {
ex = new Persistenceexception (e);
try {
Em.gettransaction (). rollback ();
} catch (Exception innerexception) {
}
} finally {
try {
Em.close ();
} catch (Exception e) {
}
}

if (ex! = null) {
Throw ex;
}

return ro;
}

Delete operation

Protected void Remove (Object object) throws Persistenceexception {
  persistenceexception ex = null;
  entitymanager em = Getentitymanager ();
  em.gettransaction (). Begin ();
  try {
   em.remove (Em.merge (object));

Em.gettransaction (). commit ();
} catch (Persistenceexception e) {
ex = e;
try {
Em.gettransaction (). rollback ();
} catch (Exception innerexception) {
}
} catch (Exception e) {
ex = new Persistenceexception (e);
try {
Em.gettransaction (). rollback ();
} catch (Exception innerexception) {
}
} finally {
try {
Em.close ();
} catch (Exception innerexception) {
}
}

if (ex! = null) {
Throw ex;
}
}

Number of queries, list

@SuppressWarnings ("Unchecked")
protected int GetCount (Class clazz) {
Entitymanager em = null;
try {
EM = Getentitymanager ();
return em! = null? GetCount (EM, clazz): 0;
} finally {
if (em! = null) {
Em.close ();
}
}
}

@SuppressWarnings ("Unchecked")
Protected <T> list<t> findentitylist (Class clazz) {
Entitymanager em = Getentitymanager ();

try {
Return (list<t>) Em.createquery (
"From" + clazz.getsimplename () + "O"). Getresultlist ();
} finally {
Em.close ();
}
}

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.