The department entity has a unidirectional OneToMany relationship to the employee entity. The developer wants to model this relationship as a java.util.map such that the key of map is true employee name. The primary key of the Employees entity is empId, an integer.
Which of the following is correct?
A.
@OneToMany (targetEntity = Employee.class)
@MapKeyClass (string.class)
map employees;
B.
@OneToMany @mapKey (name, �name�) map < integer, Employee> Employees;
C.
@OneToMany @MapKeyJoinColumn (name = �name�) map <String, Employee> employees;
D.
@OneToMany @mapsId (name = �name�) map <String, Employee> employees;
B is almost correct, I assume it contains a scanning error. but A and C also work.
B.
@OneToMany
@MapKey (name = “name”)
Map Employees;
For A to work it should had @MapkeyColumn(name=”name”)
1.If the map is specified using Java generics, the MapKeyClass annotation and associated type need not be specified; otherwise they must be specified.
2.The MapKey annotation is not used when MapKeyClass is specified and vice versa.
3.The MapKeyJoinColumn annotation is used to specify a mapping to an entity that is a map key. The map key join column is in the collection table, join table, or table of the target entity that is used to represent the map. In this case, the map key is not an entity but a basic type from the entity kept as map value. Therefore, @MapKey or @MapKeyClass must be used.
A works, too. @MapKeyColumn is not mandatory.
We use @MapKeyColumn if the map key is a basic type. If you don’t specify the column name, the name of the property will be followed by an underscore followed by KEY (for example orders_KEY).
We use @MapKeyEnumerated / @MapKeyTemporal if the map key type is respectively an enum or a Date.
@MapKeyJoinColumn/@MapKeyJoinColumns if the map key type is another entity.
@AttributeOverride/@AttributeOverrides when the map key is a embeddable object. Use key. as a prefix for your embeddable object property names.
You can also use @MapKeyClass to define the type of the key if you don’t use generics.
Use @MapKey with one-to-many or many-to-many relationship Map that is keyed on an attribute of the target entity. To use one of the target entity properties as a key of the map, use @MapKey(name=”myProperty”), where myProperty is a property name in the target entity (entity that is the value of the java.util.Map). When using @MapKey without the name attribute, the target entity primary key is used.
Conclusion:
So basically the point of this question was to realize that if we use property of the target entity as a key, we need to use @MapKey annotation, because Employee is the target entity and Employee name is the property of the Employee entity.
And answer should be either this:
@OneToMany
@MapKey // map key is primary key
public Map employees;
,or this:
@OneToMany
@MapKey(name=”name”)
public Map employees;
Offered answer B is not correct since in @MapKey annotation we have defined name property whose type is String. According to the sentence (the key of map is true employee name) key of the map is Employee name.
So if you ask me correct answer should look like this:
@OneToMany
@MapKey(name=”name”)
public Map employees;
I have completely forgot that we cannot use triangular brackets here so I will repeat “conclusion” part of my answer by using normal brackets instead of triangular brackets:
Conclusion:
So basically the point of this question was to realize that if we use property of the target entity as a key, we need to use @MapKey annotation, because Employee is the target entity and Employee name is the property of the Employee entity.
And answer should be either this:
@OneToMany
@MapKey // map key is primary key
public Map(Integer, Employee) employees;
,or this:
@OneToMany
@MapKey(name=”name”)
public Map(String, Employee) employees;
Offered answer B is not correct since in @MapKey annotation we have defined name property whose type is String. According to the sentence (the key of map is true employee name) key of the map is Employee name.
So if you ask me correct answer should look like this:
@OneToMany
@MapKey(name=”name”)
public Map(String, Employee) employees;