• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

@ManayToMany

 
Deepika Joshi
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This question is from uCertify trial version
Two Entity classes with bi-driectional many-to-many relationship



a - @ManyToMany(mappedBy="faculty") on faculty field.
b - @ManyToMany on students field.
c - @ManyToMany(mappedBy="faculty") on students field.
d - @ManyToMany(targetEntity=Student.class) on students field.

Correct ans. - a & d.

I can understand reason for
d - @ManyToMany(targetEntity=Student.class) on students field.
since students field is not using generic it is correct to use d. (targetEntity)

But I am not able to understand how a is correct

I think I need
@ManyToMany(mappedBy="students") on faculty field.

I am totally lost here...
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The inverse side of a bidirectional relationship must refer to its owning side by use of the mappedBy element of the OneToOne, OneToMany, or ManyToMany annotation. The mappedBy element designates the property or field in the entity that is the owner of the relationship. So the mappedBy in Faculty should refer to the faculty field in Student, like written in answer "a".


(Collections students??)
 
Deepika Joshi
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for replying...

(Collections students??)

sorry its,
Collection students


The inverse side of a bidirectional relationship must refer to its owning side by use of the mappedBy element of the OneToOne, OneToMany, or ManyToMany annotation. The mappedBy element designates the property or field in the entity that is the owner of the relationship. So the mappedBy in Faculty should refer to the faculty field in Student, like written in answer "a".

as suggested, following is correct answer
a - @ManyToMany(mappedBy="faculty") on faculty field.

but In Many-to-Many owner can be any side, so can I say, following is also correct,
@ManyToMany(mappedBy="students") on faculty field


Reason for doubt is that I have not seen any example of follwing yet,
@Entity
public class Student {
........@ManyToMany(mappedBy="faculty")
........Set <Faculty> faculty;
}



 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but In Many-to-Many owner can be any side, so can I say, following is also correct,
@ManyToMany(mappedBy="students") on faculty field

Yes, this would be correct.


No, this is not correct. The container will look for a field called "faculty" in the Faculty entity, which does not exist. This must be @ManyToMany(mappedBy="students"). You should think : Which field is used in the relation on the opposite side ? The field used in Faculty for this relation is "students", so this is the field you have to use in mappedBy (if Student is the owner of the relation).


or

 
Deepika Joshi
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for your reply.



No, this is not correct.


then ans a. is not correct choice (refering to very first messgae).
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
then ans a. is not correct choice (refering to very first messgae).

You're right, I got messed up with faculties and students I would say that C was the correct answer, instead of A.
 
Deepika Joshi
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
 
Omar Al Kababji
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No C is not correct

@ManyToMany(mappedBy="faculty") on students field.


since students is not using Generics and a targetEntity should be used, and you can't annotate students with two @ManyToMany annotations. so i think that the only correct answer is D. however answer A. could have been correct if they replaced it with this one (may be they got it wrong).



 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic