Skip to content

Instantly share code, notes, and snippets.

@thecodesmith
Last active March 10, 2016 23:34
Show Gist options
  • Select an option

  • Save thecodesmith/555a0ff5084c3df2912a to your computer and use it in GitHub Desktop.

Select an option

Save thecodesmith/555a0ff5084c3df2912a to your computer and use it in GitHub Desktop.

Notes on Advanced GORM - Burt Beckwith on infoq.com

GORM Collections

Don't use collections (for large collections) Instead of using GORM collections:

class Library {
    String name
    static hasMany = [visits: Visit]
}

class Visit {
    static belongsTo = [library: Library]
}

Use this:

class Library {
    String name
}

class Visit {
    Library library
}

Then use a dynamic finder to find visits for a library:

def visits = Visit.findAllByLibrary(library)

Must delete all visits in a transactional service method when deleting library (cascading deletes lost)

Many-to-Many

Same issue with hasMany collections - Solution:

Take out collections and map the join table:

class User {
    String username
}

class Role {
    String name
}

class UserRole implements Serializable {
    User user
    Role role
    static mapping = {
        table 'user_roles'
        version false
        id composite: ['user', 'role']
    }
}

Then add "roles" pseudo-collection back to User:

class User {
    String username

    Set<Role> getRoles() {
        UserRole.findAllByUser(this).collect { it.role } as Set
    }

    boolean hasRole(Role role) {
        UserRole.countByUserAndRole(this, role) > 0
    }
}

Equals and HashCode for domain classes

Any time a domain class is going to be in a hash-based collection, it must have a well-defined hashCode and equals methods

Hibernate Query Caching

Almost never do it. Maybe sometimes in the case of read-mostly objects.

AppInfo Grails plugin

Useful. Get it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment