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.
Correct Answer: AD
Explanation/Reference:
Explanation:
A:JPA 2.0 defines an ElementCollection mapping. It is meant to handle several non-standard relationship mappings. An ElementCollection can be used to define a one-to-many relationship to an Embeddable object, or a Basic value (such as a collection of Strings). An ElementCollection can also be used in combination with a Map to define relationships where the key can be any type of object, and the value is an Embeddable object or a Basic value.
D:
Example of an ElementCollection relationship database:
Example of an ElementCollection relationship annotations @Entity
public class Employee {
@Id
@Column(name="EMP_ID")
private long id;
@ElementCollection
@CollectionTable(
name="PHONE",
joinColumns=@JoinColumn(name="OWNER_ID")
)
private List<Phone> phones;
}
@Embeddable
public class Phone {
private String type;
private String areaCode;
@Column(name="P_NUMBER")
private String number;
}
References: