Last active
August 29, 2015 14:21
-
-
Save zho/288688e34f50fd15393b to your computer and use it in GitHub Desktop.
JPA Sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.List; | |
import java.util.UUID; | |
import javax.persistence.EntityManager; | |
import javax.persistence.EntityManagerFactory; | |
import javax.persistence.Persistence; | |
import javax.persistence.Query; | |
public class TestJavaApp { | |
private static String PERSISTENCE_UNIT_NAME = "DEFAULT_PU"; | |
private static EntityManagerFactory factory; | |
/** | |
* @param args the command line arguments | |
*/ | |
public static void main(String[] args) { | |
factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME); | |
EntityManager em = factory.createEntityManager(); | |
// read the existing entries and write to console | |
Query q = em.createQuery("SELECT p FROM Pelanggan p"); | |
List<Pelanggan> todoList = q.getResultList(); | |
for (Pelanggan todo : todoList) { | |
System.out.println(todo); | |
} | |
System.out.println("Size: " + todoList.size()); | |
// create new todo | |
em.getTransaction().begin(); | |
Pelanggan p = new Pelanggan(); | |
p.setNamaPelanggan("John Doe"); | |
p.setIdPelanggan(UUID.randomUUID().toString()); | |
p.setEmailPelanggan("[email protected]"); | |
p.setAlamatPelanggan("Bekasi, Indonesia"); | |
em.persist(p); | |
em.getTransaction().commit(); | |
em.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment