wood burning stoves 2.0*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes @ManayToMany Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "@ManayToMany" Watch "@ManayToMany" New topic
Author

@ManayToMany

Deepika Joshi
Ranch Hand

Joined: Feb 24, 2009
Posts: 268
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

Joined: Nov 24, 2005
Posts: 14687
    
  16

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??)


[My Blog]
All roads lead to JavaRanch
Deepika Joshi
Ranch Hand

Joined: Feb 24, 2009
Posts: 268
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

Joined: Nov 24, 2005
Posts: 14687
    
  16

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

Joined: Feb 24, 2009
Posts: 268
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

Joined: Nov 24, 2005
Posts: 14687
    
  16

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

Joined: Feb 24, 2009
Posts: 268
Thanks
Omar Al Kababji
Ranch Hand

Joined: Jan 13, 2009
Posts: 357
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).




Omar Al Kababji - Electrical & Computer Engineer
[SCJP - 90% - Story] [SCWCD - 94% - Story] [SCBCD - 80% - Story] | My Blog
 
jQuery in Action, 2nd edition
 
subject: @ManayToMany