Using Java Persistence API [JPA] – Sample [2]

well, in this section you will try to update and remove a record in a database through Java Persistence API.

hum,… this part is a continued part of the provious section actually, so you need to do the previous [about record insertion] before continuing to this one.

updating and removing record by using JPA is so easy. in this session you just need to modify two classes that we have made before [in the previous part].

  1. first is modifying DBManager class by adding four more methods, the class will be like this:
    package simplejpa.entities.manager;
    import javax.persistence.Query;
    import javax.persistence.EntityManager;
    import javax.persistence.EntityManagerFactory;
    import javax.persistence.Persistence;
    import simplejpa.entities.Account;
    
    public class DBManager {
    
     private EntityManager em;
     private EntityManagerFactory emf;
    
     private EntityManager refreshEntityManager() {
     emf = Persistence.createEntityManagerFactory("SimpleJPAPU");
     if (em == null) {
     em = emf.createEntityManager();
     }
    
     return em;
     }
    
     public void addNewAccount(
     String _accountName,
     String _accountEmail,
     String _accountPhone,
     String _accountAddress) {
     refreshEntityManager();
     try {
     em.getTransaction().begin();
     Account account = new Account();
     account.setAccountName(_accountName);
     account.setAccountEmail(_accountEmail);
     account.setAccountPhone(_accountPhone);
     account.setAccountAddress(_accountAddress);
    
     em.persist(account);
     em.getTransaction().commit();
     } catch (Exception ex) {
     ex.printStackTrace();
     }
     }
    
     public Account getAccountWithID(Long _accountId) {
     refreshEntityManager();
     Account account = null;
     Query query = em.createQuery("SELECT A FROM Account A WHERE A.accountId=:ACCOUNTID");
     query.setParameter("ACCOUNTID", _accountId);
    
     try {
     account = (Account) query.getSingleResult();
     } catch (Exception ex) {
     ex.printStackTrace();
     }
    
     return (account);
     }
    
     public Account getAccountWithName(String _accountName) {
     refreshEntityManager();
     Account account = null;
     Query query = em.createQuery("SELECT A FROM Account A WHERE A.accountName=:ACCOUNTNAME");
     query.setParameter("ACCOUNTNAME", _accountName);
    
     try {
     account = (Account) query.getSingleResult();
     } catch (Exception ex) {
     ex.printStackTrace();
     }
    
     return (account);
     }
    
     public void updateAccountWithID(
     Long _accountId,
     String _newAccountName,
     String _newAccountEmail,
     String _newAccountPhone,
     String _newAccountAddress) {
     refreshEntityManager();
     try {
     em.getTransaction().begin();
     Account account = getAccountWithID(_accountId);
     account.setAccountName(_newAccountName);
     account.setAccountEmail(_newAccountEmail);
     account.setAccountPhone(_newAccountPhone);
     account.setAccountAddress(_newAccountAddress);
    
     em.merge(account);
     em.getTransaction().commit();
     } catch (Exception ex) {
     ex.printStackTrace();
     }
     }
    
     public void removeAccountWithID(
     Long _accountId) {
     refreshEntityManager();
     try {
     em.getTransaction().begin();
     Account account = getAccountWithID(_accountId);
    
     em.remove(account);
     em.getTransaction().commit();
     } catch (Exception ex) {
     ex.printStackTrace();
     }
     }
    }
    
  2. the next is to modifying Main class, just like this:
    package simplejpa;
    import simplejpa.entities.Account;
    import simplejpa.entities.manager.DBManager;
    
    public class Main {
    
     public static void main(String[] args) {
     // instantiate DBManager
     DBManager dbm = new DBManager();
    
     // try to add some new accounts
     dbm.addNewAccount("mario", "mariosimaremare@yahoo.co.id", "0123456789", "Sitoluama");
     dbm.addNewAccount("mario elyezer", "mariosimaremare@gmail.com", "081081081081", "Laguboti");
     dbm.addNewAccount("mario simaremare", "mariosimaremare@hotmail.com", "085208520852", "North Sumatera");
    
     // retrieve some registered accounts
     Account mario = dbm.getAccountWithName("mario");
     Account marioelyezer = dbm.getAccountWithName("mario elyezer");
    
     // try to remove account
     dbm.removeAccountWithID(mario.getAccountId());
     // try to update account
     dbm.updateAccountWithID(marioelyezer.getAccountId(), "mario elyezer subekti", "me@mycampus.com", "065506550655", "campus");
     }
    }
    
  3. ok, finish! you can run it to see what will happens…

have a good day,

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s