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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:53:01+00:00 2026-05-24T16:53:01+00:00

Assume there are two models: User and Question . I can use @OneToMany to

  • 0

Assume there are two models: User and Question. I can use @OneToMany to indicate that one user can have multiple questions. Now if I want to allow user to follow one or more questions, should I build a Follow model which has two properties, user and question? Or simply add a property into User model for the list of questions followed? Which one is better? What’s the general design principle for this kind of scenario? Considering backend database operations, which approach above would be more efficient?

  • 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-24T16:53:03+00:00Added an answer on May 24, 2026 at 4:53 pm

    The key idea here is that a question has a single asker but multiple followers. Working at the data model, where you envision everything as tables, you would design as follows:

    USER (id(PK), name, ...)
    QUESTION (id(PK), title, text, author(FK to USER))
    FOLLOW (userid(FK to USER), questionid (FK to QUESTION))
    

    Because the author relation is one-to-many from USER to QUESTION and many-to-many between USER and QUESTION. The FOLLOW table is the usual join table.

    Now doing the ORM with JPA is where things get interesting. Yes, you can avoid building a Follow class explicitly by storing a list of questions for each user. You will need @ManyToMany and @JoinTable. Is this more efficient than an explicit Follow class, perhaps with two @ManyToOnes or two @OneToManys? I’m not really sure, but I would suspect that since JPA is just a specification and there can be multiple implementations, that perhaps different implementations can, well, differ in how performant they are. You can of course try things both ways, write up some queries, and look at the generated SQL. If you are already aware of techniques for efficient fetch strategies, and have other skills like avoiding the n+1 select problem, then you may find this a fun exercise.

    Certainly, avoiding a model class for a join table is more ORM-ish since you can just focus on the object level and not worry too much about the underlying tables. But it is good to know what goes on behind the scenes as you still need to understand how to do efficient fetching and how to avoid n+1 selects, etc.

    Also, if your follow relation has attributes of its own, you may find the explicit Follow model class is a good idea.

    Bottom line is a straight many-to-many without the Follow model is probably the cleanest, but choose what is most readable for you, and do some profiling if you have any doubts. I do believe that ultimately only looking at the generated SQL will tell you what you want to know, because JPA does not specify the SQL. And JPA implementations might be pretty smart, too.

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

Sidebar

Related Questions

Specifically, let's assume that we have two sensible models: TieDyeCentipede , which has_many :legs
Let's assume we have two models: User and Token . A user may have
I've run into this problem with testing. Let's assume I have two models, User
Assume, we have a model, let's call it Articles. There, we have two columns,
I have two tables, users{name,id,age_range_id} and age_ranges{id,range_name} . There are also two models, controllers
Let's assume I have two sites around one DB. The first is based on
For the purpose of this questions, let's assume I have these two Entities defined
How do I have a script run every, say 30 minutes? I assume there
Assume I have created a compiled re: x = re.compile('^\d+$') Is there a way
Assume I have 10 Methods and 10 Properties. Is there a way to add

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.