jose pereira jose pereira - 29 days ago 18
Java Question

JPA Query clause Where

How should to be a query like this in JPA

"SELECT t FROM persona t WHERE t.value1 = :value1 AND t.value2 = :value2"


I have EntintyManager already defined:

EntityManagerFactory factory = Persistence.createEntityManagerFactory("persona");
EntityManager em = factory.createEntityManager();


wanted return 1 object type Persona same class that I using to mapping

How should be?

Answer
TypedQuery<Persona> q = em.createQuery("SELECT t FROM persona t WHERE t.value1 = :value1 AND t.value2 = :value2", Persona.class);
Persona result = q.setParameter("value1", val1)
                  .setParameter("value2", val2)
                  .setMaxResults(1)
                  .getSingleResult();
Comments