A developer wants to model the grades for a student as a Map<course, integer>. Assume that Student and Course are entitles, and that grades are modeled by integers.
Which of the following two statements are correct? (Choose two)
A.
The developer can model the grades as an element collection in the Student entity.
B.
The developer can model the grades as a oneToMany relationship in the Student entity.
C.
The mapping for the key of the map can be specified by the @MapKeycolumn annotation.
D.
The mapping for the value of the map can be specified by the @Column annotation.
A D
A,D are correct http://www.objectdb.com/api/java/jpa/MapKeyColumn
A D
“C” is incorrect the to override the key column use
@MapKeyJoinColumn(name=”newName”)
Annotation MapKeyColumn
Example:
@Entity
public class Item {
@Id int id;
…
@ElementCollection
@MapKeyColumn(name=”IMAGE_NAME”)
@Column(name=”IMAGE_FILENAME”) //mapping for the value of the map can be specified by
//the @Column annotation
@CollectionTable(name=”IMAGE_MAPPING”)
Map images; // map from image name to filename
…
}
when the map value is a basic type or embeddable class, the ElementCollection annotation is used; when the map value is an entity, the OneToMany or ManyToMany annotation is used. In this case, since map key is a basic type and map value is an entity, a OneToMany (or ManyToMany as the case may be) must be used.
Given that there is a unidirectional one to many relationship between a Customer and CreditCard, you want to model it as a Map in Customer. You want the cardNumber field (String) of the CreditCard entity to be the key of the map and the CreditCard entity to be the value. The primary key of CreditCard is cardId, which is an Integer.
@OneToMany
@MapKey(name=”cardNumber”)
private Map creditCards;
ANSWER A D
WHY C IS WRONG ?
BECAUSE :
The MapKeyColumn annotation is used to specify the mapping for the key column of a map whose map key is a basic type.
AND Course IS an entity.
Correct answer is: A, D
Following rules should be applied for the type of map mentioned in the question (Map):
Map: Map
Mapping: @ElementCollection
Key annotation: @MapKeyJoinColumn
Value annotation: @Column
For some reason you are not able to se the type of map. I assume that problem lies in triangular brackets, so I will repeat my answer without them:
Correct answer is: A, D
Following rules should be applied for the type of map mentioned in the question (Map(Course, Integer)):
Map: Map(Entity, Basic)
Mapping: @ElementCollection
Key annotation: @MapKeyJoinColumn
Value annotation: @Column