Prakash
Prakash

Reputation: 388

What is the difference between mappedBy and belongsTo for m-m relationships?

What is the difference between mappedBy and belongsTo for m-m relationships in Grails? How can the two be used in a same Domain?

Upvotes: 0

Views: 199

Answers (1)

V H
V H

Reputation: 8587

I think there are 3 with 1 that has two formats:

There is belongsTo: belongsTo=[something:Something] and belongsTo=Something or belongsTo=[Something,Another]

There is Something something

and finally hasOne=[something:Something]

When you declare Something something this actually creates a table cell called something_id

When you declare belongsTo=[something:Something] something_id isn't created and I think if I understand correctly that relationship is now managed by hibernate.

Points of reference Explain belongsTo in Grails and Grails hasOne vs. belongsTo

So in short one you manage the relationship through db and other is managed by hibernate.

How can the two be used in the same domain ?

Why would you need both if they are both pointing to the same parent ?

You can use either method for any relationship the belongsTo is a hibernate control mechanism telling it what / which way the relationship is so therefore the Parent must have Child child mapping

Where as Something something or Parent parent really has none of the above requirements. Hope it is making sense

Upvotes: 1

Related Questions