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

The Archive Base Latest Questions

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

I am having some trouble understanding the difference between @OneToMany and @ManyToMany . When

  • 0

I am having some trouble understanding the difference between @OneToMany and @ManyToMany. When I use @OneToMany it defaults to create a JoinTable and if you add the mappedBy attribute you will have bidirectional relationship between two entities.

I have a Question that may belong to many Categories, and one Category may belong to many Questions. I don’t understand if I should use @ManyToMany or @OneToMany because for me it seems exactly the same thing, but it is probably not.

Could somebody explain it?

  • 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-26T21:09:15+00:00Added an answer on May 26, 2026 at 9:09 pm

    Well, the difference is in the design you’re trying to reflect using objects.

    In your case, every Question can be assigned to multiple Categories – so that’s a sign of @*ToMany relationship. Now you have to decide if:

    • each Category can have only one Question assigned to it (it will result in a unique constraint which means that no other Category can refer the same Question) – this will be @OneToMany relationship,
    • each Category can have multiple Questions assigned to it (there will be no unique constraint in the Category table) – this will be @ManyToMany relationship.

    @OneToMany (Question -> Category)

    This relationship can be represented by join table only if you explicitly define so using @JoinTable or when it is a unidirectional relationship in which the owning side is the ‘One’ side (it means that in the Question entity you have a collection of Categories, but in the Categories you don’t have any reference to the Question).

    If you think about it, it seems quite reasonable that the join table is used. There is no other way the DBMS could save a connection between one row in Question table with multiple rows in Categories table.

    However, if you would like to model a bidirectional relationship you need to specify that the Category (‘Many’ side) is the owning side of the relationship. In this case the DBMS can create a join column with foreign key in the Category table because each Category row can be connected with only one Question.

    In this way you don’t have any join table but simple foreign keys (still, as pointed at the beginning, you can force to create the join table using @JoinTable).

    @ManyToMany

    This relationship must be represented as a join table. It basically works very similar to the unidirectional @OneToMany relationship, but in this case you may have multiple rows from Question joined with multiple rows from Categories.

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

Sidebar

Related Questions

I'm having some trouble understanding the difference between these two code segments: I allocate
I'm having some trouble understanding the difference between asynchronous and synchronous Javascript, and was
I am having some trouble understanding how to use Unix's fork() . I am
I'm having some trouble understanding how to use prepared statements, when you need to
I am learning algorithm analysis. I am having trouble understanding the difference between O,
I'm having some trouble understanding how to use coroutines properly with luabind. There's a
I'm having some trouble understanding a bit of code. I've got 2 classes Company
I'm having some trouble understanding Outlook terms (CommandBarPopup, CommandBarButton etc) like what is what
I'm new to Ruby, so I'm having some trouble understanding this weird exception problem
I've been reading through Programming Clojure, and I've been having some trouble understanding Stuarts

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.