Page 1 of 1

About Question enthuware.oce-jpad.v6.2.440 :

Posted: Sat Apr 14, 2012 9:06 am
by ETS User :: Maciej
Hi,

I've got some concerns regarding correct answers to following question:

Question:
Given that there is a unidirectional OneToMany relationship between a Customer and CreditCard, you want to model it as a Map in Customer as follows -

CreditCard has an property named cardType of enum CardType, with values such as MASTERCARD, VISA, and DISCOVER.

You want the cardType field 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.

Which of the following options correctly specify this relationship?


Correct Answers (according to ETS):


1)
@OneToMany
@MapKey(name="cardType")
private Map<CardType, CreditCard> creditCards;
[/list]

2)

@OneToMany
@MapKeyEnumerated(CardType.class)
private Map creditCards;




The 1) is just fine how about 2) should not be rather one of EnumType ?
@OneToMany
@MapKeyEnumerated(STRING)
private Map creditCards;

OR

@OneToMany
@MapKeyEnumerated(ORDINAL)
private Map creditCards;

Regards,
Maciej

Re: About Question enthuware.oce-jpad.v6.2.440 :

Posted: Sat Apr 14, 2012 9:30 am
by ETS User :: Maciej
Forget it, I've just downloaded up-to-date question bank - it's fixed.

Regards,
Maciej

Re: About Question enthuware.oce-jpad.v6.2.440 :

Posted: Sat Jan 26, 2013 10:43 am
by waserek
Question:
Given that there is a unidirectional OneToMany relationship between a Customer and CreditCard, you want to model it as a Map in Customer as follows -

CreditCard has an property named cardType of enum CardType, with values such as MASTERCARD, VISA, and DISCOVER.

You want the cardType field 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.
I'm not sure if im follow, but 'unidirectional OneToMany ' means that separate table is needed. Otherwise @OneToMany with mappedBy property should be used. Because of that, shouldnt @JoinTable annotation also be defined ?

Re: About Question enthuware.oce-jpad.v6.2.440 :

Posted: Mon Jan 28, 2013 4:55 pm
by admin
You can use @JoinTable as well if you want to override the defaults for the join table. But it is not required. A join table with default name and column names will be created.

HTH,
Paul.

Re: About Question enthuware.oce-jpad.v6.2.440 :

Posted: Tue Jan 05, 2016 1:31 pm
by fargott
IMO it would be correct to use @MapKeyEnumerated(EnumType.STRING), wouldn't it?
I tested it with Hibernate. The anwer 1 that is marked to be correct (see code below) compiles and doesn't throw any exception during application startup, but no creditcards_key column is created in the database.

Code: Select all

@OneToMany
@MapKey(name="cardType")
private Map<CardType, CreditCard> creditCards;
Omitting @MapKey(name="cardType") will generate the column and will store the enum ordinal values.
Using @MapKeyEnumerated(EnumType.STRING) will generate the column, too, and will store the enum string value.

The spec. says:
The MapKey annotation is used to specify the map key for associations of type java.util.Map
when the map key is itself the primary key or a persistent field or property of the entity that is the value
of the map.
[...]
The MapKeyEnumerated annotation is used to specify the enum type for a map key whose basic type
is an enumerated type.
Thus, even answer 1 is somehow correct a answer that omits @MapKey or uses @MapKeyEnumerated would fit better. What do think?

Re: About Question enthuware.oce-jpad.v6.2.440 :

Posted: Tue Jan 05, 2016 8:55 pm
by admin
I am so sorry but I did not understand your question. Could you please rephrase it a bit?
Paul.

Re: About Question enthuware.oce-jpad.v6.2.440 :

Posted: Wed Sep 19, 2018 9:42 am
by __JJ__
This is confusing and similar to another situation with map keys that escapes my mind.
The spec says 2 things but doesn't say what happens when both of these things apply.
In this case we seem to have a map whose key is a basic type (an enum is a basic type, yes?) but the map key is also a field in the entity that is the map's value.
I would have thought though that it is more important to specify that the map's key is to be found in the set of fields for the map's value than to specify that it is an enum. Perhaps both can be applied; the only way is to test it and observe what happens, but if it is not specced then what's observed could very well be implementation dependent.