The developer is creating a Java Persistence model for a legacy database. The database contains customer and subscriptions.
The subscriptions table has a foreign key to the Customer table, a foreign key to the magazines table, and a column that stores a java.util.Date (the subscription expiration date).
The developer wants to create entities Customer and subscription to model these tables and to represent the relationship between customer and subscription as a java.util.Map.
Which one of the following fields of the Customer entity accomplishes this task?
A.
@OneToMany
@joinColumn (name = �Customer – FK�)
Map <Magzine, Data> subscriptions;
B.
@ElementaryCollection
Map <Magzine, Data> subscriptions
C.
@OneToMany
@JoinTable (name = �Subscriptions�)
Map <Magzine, Data> subscriptions;
D.
@ElementaryCollection
@CollectionTable (name = �subscriptions�)
Map <Magazine, Data> subscriptions
E.
@ElementaryCollection
@CollectionTable (Name = �Subscriptions�)
@Temporal (TemporalType.DATE)
Map<magazine, date> subscriptions
E is correct
C is not correct because I checked it with Hibernate:
@OneToMany
@JoinTable(name = “SUBSCRIPTION”)
@Temporal(TemporalType.DATE)
Map magazin;
Caused by: org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: de.example.Customer.magazin[java.util.Date]
E is correct.
It may also have annotations for date column @Column(name=”date_column_name”) in join table.
E is correct if we use @ElementCollection
here is the tested working code
@ElementCollection
@CollectionTable(name=”SubscriptionsTable”)
@Temporal(TemporalType.DATE)
Map subscriptions;
Correct answer is E.
Following rules need to be applied for Map(key = Entity, value = Basic):
Map: Map(key = Entity, value = Basic)
Mapping: @ElementCollection
Key annotation: @MapKeyJoinColumn
Value annotation: @Column
For example:
@ElementCollection
@CollectionTable(name=”EMP_SENIORITY”)
@MapKeyJoinColumn(name=”EMP_ID”)
@Column(name=”SENIORITY”)
private Map seniorities;