JPA Multiple relationships on one field in an entity -
i beginner using jpa 2.0 , databases in general , confused few concepts. have total of 3 tables. 1 usertable, contains information user. has primary key field called user_id. other 2 tables exercisestable , foodintaketable, , each have foreign key field called user_id reference user_id in usertable. want one-to-many relationship user_id table each of 2 tables can find pull out exercise information or food information user.
pretty this: foodintaketable <-> usertable <-> exercisestable
i need bidirectional mapping usertable foodintaketable , bidirectional mapping usertable exercisestable field user_id.
the problem is, when try write code in usertable class:
@onetomany(mappedby="exercisestable.userid") @onetomany(mappedby="foodintaketable.userid") public long userid;
it's illegal because can't have 2 @onetomany annotations on same field. think it's supposed legal in normal relational database , i'm confused how translate jpa. i'm new whole concept of databases , entities in general, appreciated.
in jpa can directly reference entity objects instead of ids mapped by. try this:
- you should have entity type each of tables,
exercise
exercisestable,foodintake
foodintaketable, ,user
usertable. then
user
entity owning side of relationships, having 1 field per relationship this:@onetomany(mappedby=...) private list<exercise> exercises; @onetomany(mappedby=...) private list<foodintake> foodintakes;
Comments
Post a Comment