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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:24:03+00:00 2026-05-23T03:24:03+00:00

Let’s pretend that we want to build an eBay like app in which we

  • 0

Let’s pretend that we want to build an eBay like app in which we have an entity named Customer and an entity named Order.

From a relational perspective, I would model this as:

Customer
+----+-----+
| ID | ... |
+----+-----+

Order
+----+-------------+-----+
| ID | CUSTOMER_ID | ... |
+----+-------------+-----+

Now when I want to map this to JPA, I have multiple choices:

  1. Create a uni-directional many-to-one association from Order to Customer. IMHO, this is the closest to the relational model. The downside is that in order to find all orders for a given customer I have to write a JPQL query as a customer does not know anything about its orders. Also I cannot in an OO natural way add a new order for a customer (e.g. customer.addOrder(aNewOrder);).

  2. Create a one-to-many association from the Customer to Order. This way in order to find all the orders for a given customer I can use customer.getOders() and I can add new orders for a customer in an OO natural way. The downside is that in order to find out the customer that has placed a given order I should use JPQL.

  3. Create a bidirectional one-to-many association from Customer to Order. This way I don’t need to write any JPQL queries to retrieve all orders of a customer or the customer that has placed a given order. The downside is that is added complexity to maintain the bidirectional association.

As such I don’t see a definite argument as to whether a unidirectional association has to be used or a bidirectional association is “correct”. In other words it seems to me it all boils down to personal preference and taste of the designer/implementor of the model.

Am I right or are there rules according to which it is possible to determine the correct directionality for a given association?

  • 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-23T03:24:03+00:00Added an answer on May 23, 2026 at 3:24 am

    Much depends on your expected number of orders per customer. If you expect many orders per customer (think eBay) then having customer with all orders associated with it will not be very efficient (or will take some effort to maintain as lazy association). In this case I recommend approach #1. There is nothing wrong with writing some queries.

    However if you expect mostly customers with few orders, then bi-directional approach #3 will work just fine.

    I don’t see much value in #2 as order is always associated with one customer.

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

Sidebar

Related Questions

Let's have an example like below: package xliiv.sandbox; import android.app.Activity; import android.os.Bundle; import android.util.Log;
Let's say I have a main folder in my website named test which contains
Let's say you have a class called Customer, which contains the following fields: UserName
Let's say I don't have photoshop, but I want to make pattern files (.pat)
Let's say I have a method in java, which looks up a user in
Let me explain best with an example. Say you have node class that can
Let's say that I have a SQLite database that I create in a separate
Let's say I have thousands of users and I want to make the passwords
Let's say I have a sortable list like this: $(.song-list).sortable({ handle : '.pos_handle', axis
Let's say I have a string like this: var str = /abcd/efgh/ijkl/xxx-1/xxx-2; How do

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.