Loading data from existing connector table in JPA -
i'm beginner in jpa mapping. have simple entity (essentially 2d matrix) specifying value every product group/cost group combination.
@entity public class costdistribution { @id @generatedvalue(strategy=generationtype.auto) private long id; @notnull @onetoone private productgroup productgroup; @notnull @onetoone private costgroup costgroup; private long value; }
now i'd read-only access relevant column of matrix when productgroup loaded (i.e. load costdistribution records product_group_id equals id of current product group). cannot figure out how this. tried:
@elementcollection @collectiontable(name="cost_distribution", joincolumns=@joincolumn(name="product_group_id"))
but exception: same physical table name [cost_distribution] references several logical table names: [costdistribution], [cost_distribution]
what misunderstanding here?
@entity public class productgroup { @id @generatedvalue(strategy=generationtype.auto) private long id; @column(length=50) @notnull @size(max=50) private string name; @??? private list<costdistribution> costdistribution; }
correct answer load relevant data matrix:
@onetomany(mappedby="productgroup", fetch=fetchtype.eager) private list<costdistribution> costdistribution;
this joins in correct column connector table. example, when loading product group id=1:
select productgro0_.id id1_6_0_, productgro0_.name name2_6_0_, ..., costdistri1_.value value2_2_2_, product_group productgro0_ left outer join cost_distribution costdistri1_ on productgro0_.id=costdistri1_.product_group_id left outer join cost_group costgroup2_ on costdistri1_.cost_group_id=costgroup2_.id productgro0_.id=1
Comments
Post a Comment