Created
July 27, 2015 11:58
-
-
Save ricston-git/6d03ec1d8c3e523df409 to your computer and use it in GitHub Desktop.
This file contains 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
package com.ricston.dao; | |
import com.ricston.entities.Person; | |
import java.util.List; | |
import java.util.Random; | |
import javax.persistence.Query; | |
import javax.ws.rs.PathParam; | |
import com.ricston.entitymanager.PoeticEntityManager; | |
public class PersonDao { | |
private PoeticEntityManager em; | |
public List findAll() { | |
Query q = em.getEntityManager().createQuery("Select p From Person p"); | |
List persons = q.getResultList(); | |
return persons; | |
} | |
public List findByName(String name) { | |
Query q = em.getEntityManager().createQuery("Select p From Person p Where p.name=?1"); | |
q.setParameter(1, name); | |
List persons = q.getResultList(); | |
return persons; | |
} | |
public Person generate(String name) { | |
em.getEntityManager().getTransaction().begin(); | |
Random r = new Random(); | |
Person p = new Person(); | |
p.setName(name); | |
p.setAge(r.nextInt(75)); | |
p.setJob("developer"); | |
p.setSurname("surname"); | |
em.getEntityManager().persist(p); | |
em.getEntityManager().getTransaction().commit(); | |
return p; | |
} | |
public Person findById(int id) { | |
Query q = em.getEntityManager().createQuery("Select p From Person p WHERE p.id=?1"); | |
q.setParameter(1, id); | |
return (Person)q.getSingleResult(); | |
} | |
public PoeticEntityManager getEm() { | |
return em; | |
} | |
public void setEm(PoeticEntityManager em) { | |
this.em = em; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment