Referencing JPQL, it's not clear to me exactly how to query a table.
What I want is, if it exists, the @Entity Note
for a given messagId and group, but I don't understand the syntax.
How do I specify the messageId and group for the below query?
public void persist(Msg message) {
LOG.info("\t" + message);
LOG.info("isOpen?" + em.isOpen());
em.getTransaction().begin();
int id = message.getId();
Query q = em.createQuery("SELECT n "
+ "FROM Notes n WHERE n.messageId = :messageId "
+ "AND n.group = :group");
List results = q.getResultList();
em.getTransaction().commit();
for (Object o : results) {
LOG.info("object is \n\n" + o);
}
}
}
Use setParameter(String name, Object value)
q.setParameter("messageId", id).setParameter("group", group);
To make it better, use TypedQuery
public void persist(Msg message) {
LOG.info("\t" + message);
LOG.info("isOpen?" + em.isOpen());
em.getTransaction().begin();
int id = message.getId();
TypedQuery<Note> q = em.createQuery("SELECT n "
+ "FROM Notes n WHERE n.messageId = :messageId "
+ "AND n.group = :group", Note.class);
q.setParameter("messageId", id).setParameter("group", group);
List<Note> results = q.getResultList();
em.getTransaction().commit();
for (Note o : results) {
LOG.info("object is \n\n" + o);
}
}