Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: JPA: How to join tables efficiently?

  1. #1
    Junior Member
    Join Date
    Apr 2024
    Thanked 0 Times in 0 Posts

    Default JPA: How to join tables efficiently?

    In Java, with JPA and EclipseLink. Lets say I have 3 tables: Supplier, Order and Customer

    I want to get all customers that have an order by said supplier

    Say suppliers and customers are not directly linked. They can only be linked by joining through orders:

    An order can have one customer
    A customer can have multiple orders

    An order is also linked to a supplier A supplier can have many orders
    A order can have only 1 supplier

    One way to do it:
    In JPA once you setup your Model/Entity you can do EntityManager.find(Supplier.class, 1)

    This will return the supplier and its orders, from there we can get to the orders and get all the customers linked to that supplier.

    But this way fetches all the data, while we only want the Customers, nothing else, it puts strain on the DB and the client bandwith.

    What is a better JPA, JPQL way to fetch only the Customers data that is linked to a supplier? I know that I can do a raw query but that kind of defeats the purpose of JPA.

    Thanks in advance, Kind regards,

  2. #2
    Join Date
    Jan 2024
    Thanked 3 Times in 3 Posts

    Default Re: JPA: How to join tables efficiently?

    Certainly! To efficiently fetch only the Customers linked to a specific Supplier using JPA and JPQL, you can utilize a query with JOIN and WHERE clauses to filter the results. Here's how you can achieve it:

    public class Supplier {
    // Supplier entity definition

    public class Order {
    private Supplier supplier;

    private Customer customer;
    // Order entity definition

    public class Customer {
    // Customer entity definition

    And then, in your repository or service class, you can construct a JPQL query like this:

    public List<Customer> findCustomersBySupplier(Supplier supplier) {
    EntityManager entityManager = // Obtain EntityManager instance
    String jpqlQuery = "SELECT DISTINCT o.customer FROM Order o WHERE o.supplier = :supplier";
    return entityManager.createQuery(jpqlQuery, Customer.class)
    .setParameter("supplier", supplier)

    This JPQL query selects distinct customers associated with orders where the supplier matches the given supplier parameter. By using DISTINCT, it ensures that only unique customer instances are returned.

    This approach avoids fetching unnecessary data and efficiently retrieves only the required Customer entities linked to the specified Supplier using JPA and JPQL. If you need further help with Java assignment or any programming-related queries, there are various resources available online that can provide guidance, such as educational forums, programming communities, or professional assignment help services like ProgrammingHomeworkHelp.com.

Similar Threads

  1. Have I done this efficiently ?
    By Zack Davidson in forum Java Theory & Questions
    Replies: 11
    Last Post: June 30th, 2014, 03:38 PM
  2. Help in recoding more efficiently
    By Bob M in forum What's Wrong With My Code?
    Replies: 15
    Last Post: December 25th, 2013, 02:49 PM
  3. Delighted to join the forum
    By nandapage in forum Member Introductions
    Replies: 1
    Last Post: October 15th, 2011, 09:32 AM
  4. I need join my code with the main metod, but i dont know how.
    By vviston in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 8th, 2011, 09:58 AM
  5. Help me split and then join a file
    By babbupandey in forum What's Wrong With My Code?
    Replies: 0
    Last Post: December 13th, 2010, 12:20 PM