在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我用于映射的同一类

应该怎么办

最佳答案

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

10-08 13:56