Created
August 11, 2017 02:14
-
-
Save natansevero/9fbec41b3923bdbbc55db1ea628b76f8 to your computer and use it in GitHub Desktop.
peer instruction persistence context
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 javax.persistence.EntityManager; | |
import javax.persistence.EntityManagerFactory; | |
import javax.persistence.Persistence; | |
/** | |
* | |
* @author natan | |
*/ | |
public class JPAUtil { | |
private static final EntityManagerFactory EMF = Persistence.createEntityManagerFactory("default"); | |
public static EntityManager getEntityManager() { | |
return EMF.createEntityManager(); | |
} | |
public static void closeEntityManager() { | |
EMF.close(); | |
} | |
} |
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 com.natansevero.peer.instruction.entities.Person; | |
import java.util.List; | |
import javax.persistence.EntityManager; | |
import javax.persistence.EntityTransaction; | |
import javax.persistence.Query; | |
/** | |
* | |
* @author natan | |
*/ | |
public class PersistencePerson { | |
private final EntityManager em; | |
public PersistencePerson() { | |
em = JPAUtil.getEntityManager(); | |
} | |
public Person add(Person p) { | |
EntityTransaction tx = em.getTransaction(); | |
tx.begin(); | |
em.persist(p); | |
tx.commit(); | |
return p; | |
} | |
public Person update(Person p) { | |
EntityTransaction tx = em.getTransaction(); | |
tx.begin(); | |
em.merge(p); | |
tx.commit(); | |
return p; | |
} | |
public Person delete(Person p) { | |
EntityTransaction tx = em.getTransaction(); | |
tx.begin(); | |
em.remove(p); | |
tx.commit(); | |
return p; | |
} | |
public void listAll() { | |
Query q = em.createQuery("select p from Person p", Person.class); | |
List<Person> persons = q.getResultList(); | |
for(Person p : persons) System.out.println(p.toString()); | |
} | |
} |
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.io.Serializable; | |
import javax.persistence.Column; | |
import javax.persistence.Entity; | |
import javax.persistence.GeneratedValue; | |
import javax.persistence.Id; | |
/** | |
* | |
* @author natan | |
*/ | |
@Entity | |
public class Person implements Serializable { | |
@Id | |
@GeneratedValue | |
private Integer id; | |
@Column | |
private String name; | |
public Person() { | |
} | |
public Integer getId() { | |
return id; | |
} | |
public void setId(Integer id) { | |
this.id = id; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
@Override | |
public String toString() { | |
return "Person{" + "id=" + id + ", name=" + name + '}'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment