相关文章推荐
乖乖的弓箭  ·  不借助 Fiori ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm using JPA 2.0. Hibernate 4.1.0.Final, and Java 6. How do I write a JPA query from the following psuedo-SQL?

select max(e.dateProcessed) from Event e where e.org = myOrg

And my domain object looks like the following:

@GenericGenerator(name = "uuid-strategy", strategy = "org.mainco.subco.core.util.subcoUUIDGenerator")
@Entity
@Table(name = "sb__event",
    uniqueConstraints = { @UniqueConstraint(columnNames={"EVENT_ID"}) }
public class Event
    @Column(name = "ID")
    @GeneratedValue(generator = "uuid-strategy")
    private String id;
    @ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.REMOVE})
    @JoinColumn(name = "ORGANIZATION_ID", nullable = false, updatable = true)
    private Organization org;
    @Column(name = "DATE_PROCESSED")
    @NotNull
    private java.util.Date dateProcessed;

I know that CriteriaBuilder.greatest is involved, but I just can't figure out how to write the query. This will return all the event objects that match the organization, but that's as far as I've gotten.

final CriteriaBuilder builder = m_entityManager.getCriteriaBuilder();
final CriteriaQuery<Event> criteria = builder.createQuery(Event.class);
final Root<Event> event = criteria.from(Event.class);
criteria.select(event);
criteria.where(builder.equal(Event.get("org"), org));
results.addAll(m_entityManager.createQuery(criteria).getResultList());
JPQL is simply: 

em.createQuery("select max(e.dateProcessed) from Event e where e.org = :myOrg")
  .setParameter("myOrg", myOrg)
  .getSingleResult();

while using criteria you might have:

CriteriaBuilder qb = em.getCriteriaBuilder();
CriteriaQuery<Number> cq = qb.createQuery(Number.class);
Root<Event> root = cq.from(Event.class);
cq.select(qb.max(root.get("dateProcessed")));
cq.where(qb.equal(Event.get("org"), qb.parameter(MyOrgType.class, "myOrg")));
em.createQuery(cq).setParameter("myOrg", myOrg).getSingleResult();
                Since I want to return a "java.util.Date," I assume I change the line "CriteriaQuery<Number> cq = qb.createQuery(Number.class);" to "CriteriaQuery<Date> cq = qb.createQuery(Date.class);".  Either way, I'm getting a compile error on the line "cq.select(qb.max(root.get("dateProcessed")));".  It says "Bound mismatch: The generic method max(Expression<N>) of type CriteriaBuilder is not applicable for the arguments (Path<Object>). The inferred type Object is not a valid substitute for the bounded parameter <N extends Number>"
– Dave
                May 15, 2013 at 13:17
                I didn't notice that Max only takes Expression<N>, which is different from the JPQL max expression which will accept any path expression.  This is covered in question stackoverflow.com/questions/9616390/…
– Chris
                May 15, 2013 at 13:42
                When I used "greatest" (changing 'root.get("dateProcessed")' to 'root.get(Event_.dateProcessed)') everything worked great.
– Dave
                May 16, 2013 at 19:20

With JPQL and CriteriaBuilder

CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
javax.persistence.criteria.CriteriaQuery cq= getEntityManager().getCriteriaBuilder().createQuery();
Root<T> c = cq.from(getEntityClass());
cq.select(cb.max(c.get("id")));
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.