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

now, we will practice a simple action by using Java Persistence API to interact between a simple application and it’s database.
first of all, i use this as my specifications:
MySQL database, JDK 1.6 update 10 and NetBeans 6.5 as IDE.

let’s we start it:

  1. now create a database called ‘tutorialdb’ with only one table inside, ‘account’ table. you may see the table specification below:
  2. ok, i think there is no problem in database and table creation. now we go to the java code.
  3. open your NetBeans [i use NetBeans 6.5] and create a new project, name it as ‘SimpleJPA’.
  4. now we just need to ‘convert’ all tables into classes. how? by right clicking on the project, select ‘New’ > ‘Entity Classes from Database…’.
  5. if you have no database connection, just create a new one by choosing ‘New Database Connection’.
  6. fill all required fields, make sure that you fill all of them.
  7. then NetBeans will retrieve all tables inside the database, choose the ‘account’ then click ‘Next’ to continue…
  8. you can specified the package for this entity [and it’s recommended].
  9. after the entity generated, you’ll see some missing libraries, to fix it, you just need to add TopLink and MySQL Connector, don’t worry NetBeans provide all of them.  you just need to link it to them. how? right clicking on the project’s Libraries > Add Library. select TopLink Essential and MySQL JDBC Driver.
  10. the next step is creating a persistence unit, it’s a part of Java Persistence API, which act as the administrator of connectivity, and maintain the entity life cycle. to create a persistence unit, right clicking on the project > ‘New’ > ‘Persistence Unit’.
  11. then fill the persistence unit’s name, set the driver with TopLink [default] and select the database connection. finish.
  12. now you have a managed type of entity.
  13. hmm,… do we have all? actually, yes, but i would prefer to create a manager class which will simplify us [as the programmer] to interact with the entity object, just call it as ‘Database Manager’.
  14. okay, now create a new Java Class name it as ‘DBManager’ then write down this code:
  15. package simplejpa.entities.manager;
    
    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();
     }
     }
    }
    
  16. to test the JPA, on the Main class, write down this code:
package simplejpa;

import simplejpa.entities.manager.DBManager;

public class Main {

    public static void main(String[] args) {
        DBManager dbm = new DBManager();
        String name = "mario";
        String email = "mariosimaremare@yahoo.co.id";
        String phone = "00123456789";
        String address = "North Sumatera - Indonesia";
        dbm.addNewAccount(name, email, phone, address);
    }
}
Advertisements

One thought on “Using Java Persistence API [JPA] – Sample [1]

  1. Pingback: Using Java Persistence API [JPA] – Sample [2] « mariosimaremare

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