• Post Reply Bookmark Topic Watch Topic
  • New Topic

OneToMany vs ManyToOne  RSS feed

 
raja singh kumar
Ranch Hand
Posts: 188
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?
 
Rob Spoor
Sheriff
Posts: 21052
85
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: 188
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.
 
Campbell Ritchie
Sheriff
Posts: 55351
157
  • 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: 188
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"97"?. What is that?
 
Stephan van Hulst
Saloon Keeper
Posts: 7737
142
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!