Rohit
Rohit

Reputation: 2152

Many to MAny Object model in hibernate

In a Student and Group relationship, where a student can create a 'group' of which other students can be members, this is a many to many relationship.

Now, i need to be keep track of which student is owner/creator of the group.

So, if i was not using Hibernate ORM i would have definately created the relationship as Student group many to many rel

I would like to know if there is a better way, to avoid creating the mapping table as object in hibernate orm?

Upvotes: 0

Views: 590

Answers (1)

Marc Baumbach
Marc Baumbach

Reputation: 10473

The mapping table is definitely the way to go when developing a many-to-many relationship. If you don't want to store the isOwner in the mapping table, you could store an ownerId in the Group table and have that point to a particular Student id.

If you look at this tutorial, they don't generate a hibernate object for the many-to-many relationship, but the table still exists. You're going to need a join table of some sort to correctly represent a many-to-many relationship. By moving the isOwner into the group, you won't need the hibernate object to access the owning student since you can get to it from the Owner object.

Upvotes: 1

Related Questions