Etam
Etam

Reputation: 4673

Mapping Map with an Entity as a key in JPA

Is it possible to map:

@ManyToMany(cascade = Array(CascadeType.ALL), fetch = FetchType.EAGER)
Map<Entity, Double> quantities;

instead of:

Map<Double, Entity>

?

If not, how do you map for example Item quantities in a shop basket (without dedicated table)?

Thanks in advance, Etam.

Upvotes: 1

Views: 4182

Answers (2)

James
James

Reputation: 18379

You can using an @ElementCollection and a @MapKeyJoinColumn,

see http://en.wikibooks.org/wiki/Java_Persistence/Relationships#Map_Key_Columns_.28JPA_2.0.29

But it is kind of an unusual model, you may be better off create an Entity to map to the join table instead.

Upvotes: 2

axtavt
axtavt

Reputation: 242686

Yes, it can be mapped without additional entity, see, for example, Hibernate: mapping many-to-many to Map.

Also note that dedicated table will be used anyway, it just can be mapped as an entity, or not.

Upvotes: 1

Related Questions