Grail string identifiers - how exactly can this be done?

Can someone show me a clear, complete, 100% working way to set a string name as an identifier in a graph? I read the documents and then read all the similar rants on the Internet, but could not create a working prototype.

Here is one of my attempts to make you believe that I'm not just lazily waiting for someone to do this job)))

class User { String login static hasMany = [apps : Application] static constraints = { } static mapping = { id generator: 'assigned', name: "login" } 

}

+4
source share
2 answers

When you use a natural identifier, you should use the findBy method instead of get , as shown in this test:

 def user = new User(login: "test") assertNotNull user.save(flush: true) user = User.findByLogin("test") assertNotNull user assertEquals "test", user.login 

Alternatively, you can use a single field compound identifier :

 class User implements Serializable { String login static hasMany = [apps: Application] static constraints = { } static mapping = { id composite: ['login'] } } 

Note that the composite id classes of the domain are required to implement Serializable.

The test for a composite identifier would be:

 def user = new User(login: "test") assertNotNull user.save(flush: true) user = User.get(new User(login: "test")) assertNotNull user assertEquals "test", user.login 
+8
source

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 } // void setUsername(String username) { id = username } String getUsername() { return id } } 

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.

+3
source

All Articles