Details
-
Type:
Improvement
-
Status:
Closed
-
Priority:
Major
-
Resolution: Won't Fix
-
Affects Version/s: 1.3.7
-
Fix Version/s: None
-
Component/s: Persistence
Description
Most scaffolds and best practices use the convention <Domain Class>.get(id) method. This works for the GORM dynamic id, but when users want to use unique properties they are forced to create a new get method, then override all scaffolding to use this new method. It would be a great improvement to add logic that will attempt the original method, then attempt to get the instance via any unique properties found.
Here is an example:
Consider this domain class:
class Person { String name static constraints = { name(nullable:false, blank:false, unique: true) } }The get method should use this logic (not the grooviest, but you get the idea):
get { id -> if( id instanceof Number ){ return Person.findById(id.longValue()) }else if( id instanceof String ){ def person = Person.findByName(id) if( exchange == null ){ // Attempt string coersion to number and retrieve above... } return person; } return null }