java - JPA: How to handle multiple Entities -
i'm new jpa , have question how handle entitites. in case have 3 entities: user, group , event.
event belongs group. means there onetomany-relation. user can subscribe multiple groups means there manytomany-relation. part i'm having troubles. user can subscribe multiple events means there manytomany-relation.
user
@entity public class user { @id @generatedvalue private integer id; @embedded @onetoone @joincolumn(name = "company_location") private companylocation companylocation; @manytomany(fetch = fetchtype.lazy) @jointable( name = "user_group_subscriptions", joincolumns = @joincolumn(name = "user_id", referencedcolumnname = "id"), inversejoincolumns = @joincolumn(name = "group_id", referencedcolumnname = "id")) private list<group> subscribedgroups; ... }
group
@entity public class group { @id @generatedvalue private integer id; @onetomany(???) private list<event> events; ... }
now problem. how can have list susbcribed events in group-entity depends on user-entity? goal this:
user.getsubscribedgroups().get(0).getsubscribedevents();
try :
@entity public class event{ @id @generatedvalue private integer id; @manytoone @joincolumn(name = "your_column") private group group; @manytomany @jointable(....) private list<user> users; ... } @entity public class group { @id @generatedvalue private integer id; @onetomany(mappedby = "group") @cascade(org.hibernate.annotations.cascadetype.all) private list<event> events; ... }
Comments
Post a Comment