How to Map database table without id with grails domain

07 / Sep / 2010 by Uday Pratap Singh 5 comments

Recently I got a query regarding mapping a database table which do not have any id and version. For example the table have two varchar fields username and password nothing more than that.
Although it was something strange for me that table doesn’t have the id field. The good thing is that the username is a primary key in the table and this is not auto incremented user want to create it by his own method.

The good thing about grails is, in most of the cases you get your answer in the docs . So in this case we just need to change the id field in grails domain like this

class Test {
    String username
    String password

    static mapping = {
        id name: 'username'
        version false
        id generator: 'assigned'
    static constraints = {
        username(nullable: true)

and we are done :).

Hope it helps
Uday Pratap Singh


comments (5)

  1. Uday Pratap Singh

    Hi martin

    I guess if you create your own transient getId,setId methods and override the static get method than it will work.
    Other solution can be that you install the templates and change their implementation according to your requirement .
    The last resort can be you change the generated actions and views. As you have already did the generate-all then its all your code and you can any way modify that 🙂

  2. martin

    Hello, this works fine but when you “generate-all” and run the project generates a problem when you want to edit an item created and do not allow the error “Can not find [class] with id null”. How can we solve this? that I have to modify elements of the view and the controller to fix it? I appreciate a response. And sorry for my English.


Leave a Reply

Your email address will not be published. Required fields are marked *