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 3793018
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T12:42:41+00:00 2026-05-19T12:42:41+00:00

I am just starting out with JPA 2 criteria query API and finding it

  • 0

I am just starting out with JPA 2 criteria query API and finding it tough to learn. Looked around the net a bit, but haven’t found good examples/tutorials yet. Can someone suggest a good tutorial and/or help me with the following simple query I am trying to code?

I have a class called Transaction that has a reference to the Account that it belongs:

public class Transaction {
    private Account account;
    ...
}

public class Account {
    private Long id;
    ...
}

I need to code a query that gets all the transactions for an account given its account id. Here’s my attempt at doing this (which obviously doesn’t work):

public List<Transaction> findTransactions(Long accountId) {        
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Transaction> query = builder.createQuery(Transaction.class);
    Root<Transaction> transaction = query.from(Transaction.class);

    // Don't know if I can do "account.id" here
    query.where(builder.equal(transaction.get("account.id"), accountId));
    return entityManager.createQuery(query).getResultList();
}

Can someone point me in the right direction?

Thanks.
Naresh

  • 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-05-19T12:42:42+00:00Added an answer on May 19, 2026 at 12:42 pm

    Solution:-

    public List<Transaction> findTransactions(Long accountId) { 
            CriteriaBuilder builder = entityManager.getCriteriaBuilder();
            CriteriaQuery<Transaction> query = builder.createQuery(Transaction.class);
            Root<Transaction> _transaction = query.from(Transaction.class);
    
            Path<Account> _account = _transaction.get(Transaction_.account);
            Path<Long> _accountId = _account.get(Account_.id);
    
            query.where(builder.equal(_accountId, accountId));
            return entityManager.createQuery(query).getResultList();
        }
    

    To understand the meaning of above code please read:- Dynamic, typesafe queries in JPA 2.0

    And to understand/generate JPA Metamodel please read:- Hibernate Metamodel Generator Reference Guide

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just starting out in asp.net. Have just created a login.aspx page in my site
I'm just starting out playing around with Linq Expressions and I've hit a wall.
Just starting out with my first commercial app for the iStore but dont have
I'm just starting out with Java, but my boss is pushing for this. I've
I am just starting out learning to build interfaces, but the book I am
I am just starting out with ADO.net Entity Framework I have mapped two tables
I Just starting out w/ ASP.NET MVC 3 and I am trying to render
i'm just starting out with asp.net mvc. It's a long way before you can
I am just starting out with Objective-C but have done a bunch of C++.
I'm just starting out with ASP.NET, all my prior experience has been with desktop

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.