Page 1 of 1

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

Posted: Mon Jan 02, 2012 4:21 am
by ETS User
An embeddable class may NOT be the key of a map annotated with @ElementCollection.

Can someone point me to the part of specification in 2.5 chapter that says something like this is true? I believe there are no constraints for embeddable class becoming a key in map

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

Posted: Mon Jan 02, 2012 7:27 am
by admin
Hello,
This option is a wrong option. So yes, an embeddable class can be the key of a map. The explanation makes it even clearer, "When a map field is annotated with @OneToMany (or @ManyToMany), this means the map value is an Entity. The map key can be a basic type or an embeddable."

HTH,
Paul.

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

Posted: Tue Jan 10, 2012 12:21 pm
by EricOX
Hello Paul,

I think Paul is asking the option 3 "An embeddable class may NOT be the key of a map annotated with @ElementCollection.". And you are explaining the option 2 "An embeddable class may NOT be the key of a map annotated with @OneToMany.".
I think both option 2 and 3 are not correct.

Thanks

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

Posted: Sun Mar 15, 2015 10:07 am
by ikotev
Hello,
"When a map field is annotated with @OneToMany (or @ManyToMany), this means the map value is an Entity. The map key can be a basic type or an embeddable."
I guess map key can also be an Entity as well right ?

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

Posted: Sun Mar 15, 2015 10:42 am
by admin
kotev_@abv.bg wrote:Hello,
"When a map field is annotated with @OneToMany (or @ManyToMany), this means the map value is an Entity. The map key can be a basic type or an embeddable."
I guess map key can also be an Entity as well right ?
No, if you have an entity as map key, then you are basically trying to create a relationship between 3 entities at the same time (because your map value is an entity as well). But the annotation @OneToMany is for relationship between two entities only.

HTH,
Paul.

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

Posted: Sun Mar 15, 2015 12:10 pm
by ikotev
Hi Paul,

Exactly. With Map you made relationship between three entities. To my understanding this is correct. I have the following mapping in Department entity:

@OneToMany(mappedBy = "department")
Map<Office, Employee> employees;

On Employee side:

@ManyToOne
Department department

As a result through department field I can queue employees map by office key.

In specification "2.7 Map Collections" is written that:
The map key and the map value independently can each be a basic type, an embeddable class, or an entity.
9 possible map types are available and one of them is Map<Entity, Entity>.

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

Posted: Sun Mar 15, 2015 8:55 pm
by admin
Yes, you are right. I have updated the explanation. It still remains an incorrect option though.
thank you for your feedback!
Paul.