1Z0-900 · Question #21
Given the code fragments: Which action completes this composite primary key implementation?
The correct answer is D. Add @Embeddable annotation at line 1 and @EmbeddedId(ContactId.class) at line 2.. Option D correctly implements the @EmbeddedId composite key strategy: @Embeddable must annotate the ContactId class (marking it as an embeddable value type that JPA can map), while @EmbeddedId on the entity's ContactId field tells JPA that this single embedded object represents t
Question
Given the code fragments:
Which action completes this composite primary key implementation?
Exhibit
Options
- AAdd @IdClass annotation at line 1.
- BAdd @Embeddable annotation at line 1 and replace both @Id annotations with @EmbeddedId
- CAdd @IdClass(ContactId.class) annotation at line 2.
- DAdd @Embeddable annotation at line 1 and @EmbeddedId(ContactId.class) at line 2.
How the community answered
(17 responses)- A12% (2)
- B6% (1)
- D82% (14)
Explanation
Option D correctly implements the @EmbeddedId composite key strategy: @Embeddable must annotate the ContactId class (marking it as an embeddable value type that JPA can map), while @EmbeddedId on the entity's ContactId field tells JPA that this single embedded object represents the full composite key. Together, these two annotations form a complete and valid implementation.
Why the distractors fail:
- A is wrong because
@IdClassbelongs on the entity class (line 2), not the ID class (line 1), and even then it requires@Idon each individual key field in the entity. - B is wrong because
@EmbeddedIdreplaces the singleContactIdfield in the entity - you don't annotate multiple@Idfields; those belong to the@IdClassstrategy, not@EmbeddedId. - C is wrong because
@IdClass(ContactId.class)alone is insufficient - the entity still needs@Idon each individual key field, and theContactIdclass must have matching fields (no@Embeddableneeded for this approach, but more annotations are required).
Memory tip: Think EMBEDdable → EMBEDdedId - if the key class is @Embeddable, the entity field must be @EmbeddedId. If you see @IdClass, look for individual @Id annotations on the entity fields instead.
Topics
Community Discussion
No community discussion yet for this question.
