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)
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
}
}
Any time a domain class is going to be in a hash-based collection, it must have a well-defined hashCode and equals methods
Almost never do it. Maybe sometimes in the case of read-mostly objects.
Useful. Get it.