java - Hibernate: mapping many-to-many to Map -
i developing application deals 2 following entities: products (let's name x, y, z) , materials (a, b, c, ...). it's known every product has recipe indicates materials required making product. example, produce 1 x need 2 a, 6 c , 4 d (x = 2a + 6c + 4d).
that's how reflects in database tables:
products id int name varchar ... materials id int name varchar ... recipes product_id int material_id int count int
the "count" field in third table coefficient materials of same kind (2, 6, 4 example).
so want compose product class way:
public class product { ... private map<material, integer> recipe; // how many units of each material need? ... }
is way fetch necessary data recipe map using hibernate? separate configuration approach (without annotations) preferred.
since nobody posted solution without annotations, i'll show solution jpa 2.0 @elementcollection
annotation:
@elementcollection @collectiontable(name = "recipes", joincolumns = @joincolumn(name = "product_id")) @mapkeyjoincolumn(name = "material_id") @column(name = "count") private map<material, integer> recipe;
also note since class of values of map integer
, solution without annotations documented "collection mapping" rather "entity relationship mapping".
Comments
Post a Comment