Search code examples
grailsormgrails-orm

GORM - Model Relationships Between Like Entities


For my project I'm using Grails, but this is kind of a general ORM question.

I'm new to ORMs and ER diagrams, and trying to figure out how to best describe/implement the following:

I have 2 Entities: User, Connection

A Connection consists of 2 Users (and other primitive attributes). A user can be found in more than one Connection. How would you describe the relationship between User and Connection? I don't know what you would call it, 2 to many? How would you draw it in an ER diagram.

In GORM, should it be a bi-directional relationship?

Edit

As an added requirement, assume the users have specific roles in the relationship. Like student and teacher. So Connection would have student and teacher properties of type User.


Solution

  • I think what you want can be described as a many-to-many relationship, with some sort of constraint applied on Connection users collection. As for uni/bi-directional, I believe you can accomplish either depending on your use case - although uni-directional is probably harder to map/configure.

    I haven't tested this, but a bi-directional many-to-many should look like this

    class Connection {
      ...
      Set users = [] //ensure users exists, constraints won't work on null 
      static belongsTo = [User] 
      static hasMany = [users: User]
    
      static constraints = {
        users maxSize: 2
      }
    }
    
    class User {
      ...
      static hasMany = [connections: Connection]
    }
    

    I don't have a uni-directional example off the top of my head, but I believe you may need to use an additional mapping class.

    Again, I haven't tested this, but here's an example of uni-directional many-to-many with a mapping class

    class Connection {
      ...
      Set users = []
      static hasMany = [users: ConnectionUser]
      static constraints = {
        users maxSize: 2
      }
    }
    
    class User {
      ...
    }
    
    class ConnectionUser {
      User user
      Connection connection
      //or for cascading effects
      //static belongsTo = [connection: Connection]
      ...
      ... //lots of additional code to manage adding,
      ... //removing, updating Connection-to-User relationships
    } 
    

    Connection knows about Users, but Users do not know about Connections they belong to. If you go the mapping class route, you'll need to manage the Connection to User relationships manually. Take a look at Spring Security Core PersonAuthority class for an example of mapping class.