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

  • Home
  • SEARCH
  • 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 715179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:09:29+00:00 2026-05-14T05:09:29+00:00

In the example section of the @OneToMany JPA annotation reference : Example 1-59 @OneToMany

  • 0

In the example section of the @OneToMany JPA annotation reference:

Example 1-59 @OneToMany – Customer Class With Generics

@Entity
public class Customer implements Serializable {
    ...
    @OneToMany(cascade=ALL, mappedBy="customer")
    public Set<Order> getOrders() { 
        return orders; 
    }
    ...
}

Example 1-60 @ManyToOne – Order Class With Generics

@Entity
public class Order implements Serializable {
    ...
    @ManyToOne
    @JoinColumn(name="CUST_ID", nullable=false)
    public Customer getCustomer() { 
        return customer; 
    }
    ...
}

It seems to me that the Customer entity is the owner of the association. However, in the explanation for the mappedBy attribute in the same document, it is written that:

if the relationship is bidirectional,
then set the mappedBy element on the
inverse (non-owning) side of the
association to the name of the field
or property that owns the relationship
as Example 1-60 shows.

However, if I am not wrong, it looks like in the example, the mappedBy is actually specified on the owning side of the association, rather than the non-owning side.

So my question is basically:

  1. In a bidirectional (one-to-many/many-to-one) association, which of the entities is the owner? How can we designate the One side as the owner? How can we designate the Many side as the owner?

  2. What is meant by “the inverse side of the association”? How can we designate the One side as the inverse? How can we designate the Many side as the inverse?

  • 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-14T05:09:30+00:00Added an answer on May 14, 2026 at 5:09 am

    To understand this, you must take a step back. In OO, the customer owns the orders (orders are a list in the customer object). There can’t be an order without a customer. So the customer seems to be the owner of the orders.

    But in the SQL world, one item will actually contain a pointer to the other. Since there is 1 customer for N orders, each order contains a foreign key to the customer it belongs to. This is the “connection” and this means the order “owns” (or literally contains) the connection (information). This is exactly the opposite from the OO/model world.

    This may help to understand:

    public class Customer {
         // This field doesn't exist in the database
         // It is simulated with a SQL query
         // "OO speak": Customer owns the orders
         private List<Order> orders;
    }
    
    public class Order {
         // This field actually exists in the DB
         // In a purely OO model, we could omit it
         // "DB speak": Order contains a foreign key to customer
         private Customer customer;
    }
    

    The inverse side is the OO “owner” of the object, in this case the customer. The customer has no columns in the table to store the orders, so you must tell it where in the order table it can save this data (which happens via mappedBy).

    Another common example are trees with nodes which can be both parents and children. In this case, the two fields are used in one class:

    public class Node {
        // Again, this is managed by Hibernate.
        // There is no matching column in the database.
        @OneToMany(cascade = CascadeType.ALL) // mappedBy is only necessary when there are two fields with the type "Node"
        private List<Node> children;
    
        // This field exists in the database.
        // For the OO model, it's not really necessary and in fact
        // some XML implementations omit it to save memory.
        // Of course, that limits your options to navigate the tree.
        @ManyToOne
        private Node parent;
    }
    

    This explains for the “foreign key” many-to-one design works. There is a second approach which uses another table to maintain the relations. That means, for our first example, you have three tables: The one with customers, the one with orders and a two-column table with pairs of primary keys (customerPK, orderPK).

    This approach is more flexible than the one above (it can easily handle one-to-one, many-to-one, one-to-many and even many-to-many). The price is that

    • it’s a bit slower (having to maintain another table and joins uses three tables instead of just two),
    • the join syntax is more complex (which can be tedious if you have to manually write many queries, for example when you try to debug something)
    • it’s more error prone because you can suddenly get too many or too few results when something goes wrong in the code which manages the connection table.

    That’s why I rarely recommend this approach.

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

Sidebar

Related Questions

The example code of section 10.6, the expected result is: after several iterations, the
I follow example from here using section listview http://lalit3686.blogspot.com/2012/05/sectionadapter.html But how can I implement
Trying to follow this example. (Section String sorting...) Is there anything obvious that would
Example Div: <div class=container> <div class=select1></div> <div class=select2></div> <div class=select3></div> </div> Now I want
How to concatenate strings in python? For example: Section = 'C_type' Concatenate it with
The URLs like below are supported on my site: (1) http://mysite.example.com/section/ - main page
According to numerous sources, for example Limitations section on official page , probably the
The C# 3.0 spec has the following code example in section 10.6.1.3 Output parameters:
In LaTeX you can easily reference a section by using \label{} next to a
what size should I export my graphics, for use in the example/screenshot section of

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.