More Than Five
More Than Five

Reputation: 10419

Unidirectional M:M in Grails GORM

Is it possible to have a Unidirectional M:M in Gorm?

e.g. I have a Person object and I have TravelDestionion object.

A person can have been to many travel destination and some of these travel destinations have of course had many people. But, I never want to navigate from TravelDestination to Person.

Any tips?

Upvotes: 0

Views: 50

Answers (1)

Burt Beckwith
Burt Beckwith

Reputation: 75671

You can technically just not access the other side, but Grails is too helpful in this regard. For example when you want to associate a new TravelDestination instance with a Person, you add it to the person's destinations collection (or whatever you named it). But to ensure that both sides are in sync with what the database will look like after calling save, Grails adds the Person to the TravelDestination's persons collection.

This can obviously be very expensive, e.g. with a User <-> Role relationship where 1,000,000 users have ROLE_USER, and one more gets that role, and that user becomes element #1,000,001 in the role's users collection, which gets loaded into memory by Hibernate to ensure uniqueness.

Check out this presentation where I discuss this and provide some performant options for reconfiguring the relationship without the potentially huge overhead of using collections to represent 1-many and many-many in GORM: http://www.infoq.com/presentations/GORM-Performance

Upvotes: 1

Related Questions