(JavaEE)Example JPA Part 1

Salam Super, postingan sebelumnya sudah saya sejaskan apa itu JPA dan apa gunanya bisa dateng ke https://aeroyid.wordpress.com/2013/06/11/javaeejpa-java-persistanse-api/

Create Project dengan nama ExampleJPA1

Create Data base terlebih dahulu

Masuk Ke server tab dan connect kan data base yang akan di gunakan

4

Create Entity Class dengan nama Person dan gunakan persisten JPA versi 2

3

Edit class entitas seperti di bawah ini

 /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jpaexample1;

import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

/**
 *
 * @author Victor
 */
@Entity
public class Person2 implements Serializable {
     private Long id;
    private String name;
    private String alamat;
    private String no;
    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }
    public void setName(String name){
        this.name = name;
    }
    public String getName(){
        return name;
    }
    public void setAlamat(String alamat){
        this.alamat = alamat;
    }
    public String getAlamat(){
        return alamat;
    }
    public void setNo(String no){
        this.no = no;
    }
    public String getNo(){
        return no;
    }
    
}

Buka class main dan tuliskan code dibawah ini

 /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jpaexample1;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
/**
 *
 * @author Victor
 */
public class JPAExample1 {
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
    Person2 pr = new Person2();
    pr.setName("Dede");
    pr.setAlamat("Jogja");
    pr.setNo("089787878");
    
    JPAExample1 ad = new JPAExample1();
    ad.persist(pr);
    }
    
    public void persist(Object object){
        EntityManagerFactory emf = javax.persistence.Persistence.createEntityManagerFactory("JPA");
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        try {
            em.persist(object);
            em.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
            em.getTransaction().rollback();
        }finally{
            em.clear();
        }
    }
}

sehingga semua yang ada di project :
6

Run dan hasilnya dapat dilihat di server project

1

Advertisements

Published by

jarcode

hello I am jarcode

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