Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8629183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:44:12+00:00 2026-06-12T08:44:12+00:00

Sorry for this rather basic question, but I have to get some sort of

  • 0

Sorry for this rather basic question, but I have to get some sort of prototype working very quickly and this is my first foray into JPA.

I have a class, System that has a List of Snapshot items, each has a numeric ID, and a SystemID.

How do I query Snapshots to say something like:

select top 1 ID from Snapshots
where Snapshots.SystemID = X 
order by Snapshots.ID desc; 

I know how to put the where query in, not sure where to put my “greatest” bit.

Thanks!!

public Snapshot GetMostRecentSnapshotByID(int systemID) {

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<mynamespace.Snapshot> criteria = 
            cb.createQuery(mynamespace.Snapshot.class);
    Root<mynamespace> snapshot = criteria.from(mynamespace.Snapshot.class);
    criteria.where(cb.equal(snapshot.get(Snapshot_.systemID), systemID));

    //OK -- where does this guy go?
    cb.greatest(snapshot.get(Snapshot_.id));

    return JPAResultHelper.getSingleResultOrNull(em.createQuery(criteria));
}

Clarification: I have the following (snippet) of my snapshot class
@

Entity
public class Snapshot implements Serializable {



    @Id
    @GeneratedValue
    private int id;

    @ManyToOne
    @JoinColumn(name = "systemID", nullable = false)
    private System system;

Can I query against a numerical id, vs using a System object, to find a particular System’s snapshots?

Sorry if that was confusing!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T08:44:13+00:00Added an answer on June 12, 2026 at 8:44 am

    You are a bit confused about jpa working with entities and properties instead of tables and columns; if you are learning I suggest you to first try to implement your query using jpql, something like:

    String q = "from Snapshot s where s.systemID = :systemID order by s.id desc";
    TypedQuery<Snapshot> query = em.createTypedQuery(q, Snapshot.class);
    query.setParameter("systemID", systemID);
    return query.getFirstResult();
    // return a Snapshot object, get the id with the getter
    

    (it would have been better to map (@OneToMany) Snapshot to System entity instead of using primitive ID)

    then you could make a try with CriteriaBuilder (not using metamodel here):

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Object> cq = cb.createQuery();
    Root<Snapshot> r = cq.from(Snapshot.class);
    cq.where(cb.equal(r.get("systemID"), systemID));
    cd.orderBy(cb.desc(r.get("id")));
    em.createQuery(cq).geFirsttResult();
    

    if you wanted to make a where...and... (but it’s not your case in this question), it would have been:

    [...]
    Predicate p1 = cb.equal(r.get("systemID"), systemID));
    Predicate p2 = cb. /* other predicate */
    cb.and(p1,p2);
    [...]
    

    EDIT:

    Can I query against a numerical id, vs using a System object, to find
    a particular System’s snapshots?

    Sure, you can do it like that (given that System has an @Id property named id):

    String q = "from Snapshot s where s.system.id = :systemID order by s.id desc";
    [...]
    

    where s.system.id means: property id (integer) of the property system (class System) of s (Snapshot).

    Or, if you had the System entity, you could compare directly the objects:

    String q = "from Snapshot s where s.system = :system order by s.id desc";
    query.setParameter("system", system);
    [...]
    

    Using CriteriaBuilder (and metamodel):

    Metamodel m = em.getMetamodel();
    Root<Snapshot> snapshot = cq.from(Snapshot.class);
    Join<Snapshot, System> system = snapshot.join(Snapshot_.system);
    cq.where(cb.equal(System_.id, systemID));
    [...]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First of all, sorry if this is an obvious question, but I'm rather new
Sorry if this question is too vague, but I'd rather not muddy it's point
Sorry this is a basic question, but all my research just barely missed answering
Sorry that this is a rather large question. I cannot get the following C++
I am sorry for the very newbie question, but this is driving me mad.
Sorry in advance if this is a really basic Matrix/OpenGl question. I have a
sorry if this is a basic question! up till now i have been creating
Sorry this is not a very well defined question, I am thinking about an
Sorry this is probably super basic. But in all my javabean examples, I've not
sorry this is such a simple question but I can't figure it out. How

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.