Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

OneToMany vs ManyToOne  RSS feed

 
Ranch Hand
Posts: 202
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was asked in an interview what is the difference between onetomany and manytoone. I said it depends on the way we look at it. A onetomany from one side is a manytoone from the other side. He was asking if there is a difference in the queries generated? I had no reply. Can someone explain with a simple example?
 
Sheriff
Posts: 21330
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no one single correct answer to that question, because it depends on the JPA implementation. Hibernate may do it one way and EclipseLink may do it a completely different way.

But let's say we have two entities, Employee and Company. There is a bi-directional relationship from Employee (many) to Company (one) and vice versa. Let's also say that the relationship is eager in both directions (if not, then usually queries only query the table itself, and the other table is only queried if the relationship is accessed in code).

When retrieving an Employee, the query may be something like this (I'm using * for simplicity; usually the columns are listed separately):

However, when retrieving a Company, the query might be something like this:
or perhaps like this (and then the JPA provider's code groups it all on Company):
 
raja singh kumar
Ranch Hand
Posts: 202
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know why in interview they ask such questions which don't have a definite answer.
 
Marshal
Posts: 59124
180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If an interview question has a definite answer, you can say, “97,” and they have to move on to the next question. Questions with no definite answer allow them to see how you handle them; the way you say it matters at least as much as what you say. It also shows that you have thought about that particular question.
 
raja singh kumar
Ranch Hand
Posts: 202
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"97"?. What is that?
 
Saloon Keeper
Posts: 8908
167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A definite answer. Campbell used it as an example of an answer to a question such as "What is 88 + 9 ?". This is a type of question that tests knowledge, but not problem solving. Good interviewers focus on questions that test problem solving.
 
Do you pee on your compost? Does this tiny ad?
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!