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

Moderator: admin

Post Reply
ETS User :: Maciej

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

Post 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

ETS User :: Maciej

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

Post by ETS User :: Maciej »

Forget it, I've just downloaded up-to-date question bank - it's fixed.

Regards,
Maciej

waserek

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

Post 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 ?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

fargott
Posts: 3
Joined: Sun Jan 03, 2016 6:10 pm
Contact:

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

Post 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?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

I am so sorry but I did not understand your question. Could you please rephrase it a bit?
Paul.
If you like our products and services, please help us by posting your review here.

__JJ__
Posts: 125
Joined: Thu Jul 05, 2018 6:44 pm
Contact:

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

Post 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.

Post Reply

Who is online

Users browsing this forum: No registered users and 22 guests