2

I have read tons of questions about this and it seems that nobody gets it to work.

I am using grails, and I am creating a class that doesn't have id as the primary key.

I get the message "usuario not found with id null". this is the code of my domain class:

    class Usuario implements Serializable{ 

     String nombre 
     String celular 
     String telefono 
     String apellido 
     String password 
     String nick 

     static mapping = { 
      table 'Usuarios' 
      version false 
      id composite: ['nick'] 
} 
} 

I also tried the normal way with the:

    static mapping = { 
    table 'Usuarios' 
    version false 
     id name: 'nick' 
} 

It actually maps the table the way I want to with the natural key and everything, it inserts the new usuarios, but the gui is incapable of retrieving any objects and shows the error: "usuario not found with id null"

I tried to modify the show(Long id) method, but it wont help either, this is my show method from the controller:

    def show(String nick) {
    def usuarioInstance = Usuario.get(nick)
    if (!usuarioInstance) {
        flash.message = message(code: 'default.not.found.message', args: [message(code: 'usuario.label', default: 'Usuario'), nick])
        redirect(action: "list")
        return
    }

    [usuarioInstance: usuarioInstance]
}
Toon Krijthe
  • 52,876
  • 38
  • 145
  • 202
Juan Sebastian
  • 968
  • 1
  • 7
  • 20

1 Answers1

2

You need to specify the assigned generator:

static mapping = {
    ...
    id column: 'nick', generator: 'assigned'
}

Plus it might be wise to add the following constraints:

static constraints = {
    nick blank:false, nullable:false, unique: true
}
heikkim
  • 2,955
  • 2
  • 24
  • 34
  • i assigned the generator too, it still gives me the same error. i mean, it inserts the new "usuario", but it won't retrieve it. it still gives the same "usuario not found with id null" even though table usuario doesn't have any id property – Juan Sebastian Nov 22 '12 at 07:29
  • It might be that you have to rename your **nick**-property to **id** and: 1) rename mapping & constraints appropriately, 2) create a getter and setter for nick (that manipulates id-field), 3) add **static transients = ['nick']** (to go around the problem of having setter&getter but no field) – heikkim Nov 22 '12 at 08:57
  • Ok i finally got this to work, the problem was indeed the controller and the views that kept redirecting and calling methods using usuarioInstance.username : my mapping was static mapping = { table 'Usuario' version false id name: 'username', generator: 'assigned'} – Juan Sebastian Nov 23 '12 at 00:25
  • And i changed the method "show" on the controller see code below: def show(String id) { def usuarioInstance=Usuario.findByUsername(id) if (!usuarioInstance) { flash.message = message(code: 'default.not.found.message', args: [message(code: 'usuario.label', default: 'Usuario'), id]) redirect(action: "list") return } [usuarioInstance: usuarioInstance] } – Juan Sebastian Nov 23 '12 at 00:29
  • 1
    If anyone wants more information about how to get this to work can pm me, ill help anyone – Juan Sebastian Nov 23 '12 at 00:31