I assume that you are talking about converting a String primary key (usually a natural key) into a primary key into a grails domain object.
This answer is obtained from the information found here: http://dsommerville.blogspot.com/2009/09/mapping-natural-keys-using-gorm.html and here: http://gr8fanboy.wordpress.com/2010/04 / 08 / adding-a-natural-key-to-a-database-table-using-straight-gorm /
For example, you have a Users table defined with the following schema:
username varchar(40) not null pimary key, firstname varchar(40) not null, lastname varchar(40) not null
To do this in the grail, you need to massage the definition a bit. First, you need to map the identifier to this column in the database. With the generator "assigned"
Then, for ease of use, you can add the username of the transition field so that you can use user.username =. Otherwise, I believe you will need to access the field using id. The getter and setter for this transient property sets the corresponding id field, which in turn updates the database.
class User { String id String password String fullName static transients = ['username'] static constraints = { id(unique:true,blank:false) password(nullable:true,maxSize:20) fullName(nullable:true,maxSize:20) } static mapping = { table 'users' id column: 'username', generator: 'assigned' version false }
Note. Forests do not recognize the transition field, so you will have to work with the created controllers / views if you want your code to more accurately model your db.
source share