Tapas Bose
Tapas Bose

Reputation: 29806

Hibernate class design, persisting List and HashMap

In my application, I have two classes: Activity and User. The relationship among them are:

I am unable to understand should there be any ManyToOne or OneToMany or ManyToMany relationship between User and Activity. Or should I simple store the whole List<User> object along with Activity and Map<String, String> with User in database. If so, how can I store a whole List and a Map.

Any pointer would be very helpful for me.

Upvotes: 0

Views: 141

Answers (2)

Bimalesh Jha
Bimalesh Jha

Reputation: 1504

User <-> Activity is many to many. Navigability can be bidirectional- choice is yours. You also need a UserActivityMap object with three attributes (userId,ActivityId, roleId). RoleId can be an enum or an id of a RoleObject. In case of latter, you can define a lookup table and map RoleObject to that.

Upvotes: 1

JB Nizet
JB Nizet

Reputation: 691715

I would first think about how the design would be in database. If I understand correctly, you would have 3 tables:

  • User
  • Activity
  • Participation

where Participation would have a foreign key to a user, a foreign key to an activity, and a role.

So I would map that simply as

  • User has a OneToMany with Participation
  • Activity has a OneToMany with Participation

And of course you can make these two associations bidirectional.

Upvotes: 1

Related Questions