JPA Query子句Where

时间:2015-07-07 22:19:00

标签: java jpa

如何在JPA

中成为这样的查询
"SELECT t FROM persona t WHERE t.value1 = :value1 AND t.value2 = :value2"

我已经定义了EntintyManager:

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

想要返回1对象类型Persona与我用于映射的类相同

应该如何?

1 个答案:

答案 0 :(得分:1)

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();