Quantcast

GORM problem with String primary key

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

GORM problem with String primary key

pbwebguy
I have been struggling with this for a couple of hours and read all of the GORM documentation backwards and forwards countless times but I just don't get it.

What I want to do is map a Domain object to match the following IDL:
       CREATE TABLE `foo` (                        
          `role_type_code` varchar(20) NOT NULL,    
          `description` varchar(255) default NULL,  
          PRIMARY KEY  (`role_type_code`)          
        ) ENGINE=InnoDB DEFAULT CHARSET=latin1

The primary key in this case is a String.

I have tried a number of different variations of the following code but no matter what I do, I always get the error message:

org.codehaus.groovy.grails.exceptions.GrailsDomainException: Identity property not found, but required in domain class [RoleType]

class RoleType {
        String roleTypeCode
        String id
        String description

        static constraints = {
                 roleTypeCode(blank:false, nullable:false, maxLength:20)
                 description(blank:true, nullable:true)
         }

        static mapping  = {
                version false
                id column:'role_type_code', generator:assigned, unique:true
                columns {
                        roleTypeCode sqlType:'varchar(20)'
                        description sqlType:'varchar(255)'
                }
        }
       
// static id = {
// idMapping(name:'id', column:'role_type_code', unsavedValue:''), generator(class:'assigned')
// }
}

Anybody have a clue what I am doing wrong?

Thanks,

John
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: GORM problem with String primary key

burtbeckwith
http://www.nabble.com/GORM-DSL-mappings-for-natural-keys--(and-no-surrogate-%22id%22-field)-td20432533.html

--
Burt

On Thursday 12 February 2009 12:04:39 pm pbwebguy wrote:

>
> I have been struggling with this for a couple of hours and read all of the
> GORM documentation backwards and forwards countless times but I just don't
> get it.
>
> What I want to do is map a Domain object to match the following IDL:
>        CREATE TABLE `foo` (                        
>           `role_type_code` varchar(20) NOT NULL,    
>           `description` varchar(255) default NULL,  
>           PRIMARY KEY  (`role_type_code`)          
>         ) ENGINE=InnoDB DEFAULT CHARSET=latin1
>
> The primary key in this case is a String.
>
> I have tried a number of different variations of the following code but no
> matter what I do, I always get the error message:
>
> org.codehaus.groovy.grails.exceptions.GrailsDomainException: Identity
> property not found, but required in domain class [RoleType]
>
> class RoleType {
> String roleTypeCode
> String id
> String description
>
> static constraints = {
> roleTypeCode(blank:false, nullable:false, maxLength:20)
> description(blank:true, nullable:true)
> }
>
> static mapping  = {
> version false
> id column:'role_type_code', generator:assigned, unique:true
> columns {
> roleTypeCode sqlType:'varchar(20)'
> description sqlType:'varchar(255)'
> }
> }
>
> // static id = {
> // idMapping(name:'id', column:'role_type_code', unsavedValue:''),
> generator(class:'assigned')
> // }
> }
>
> Anybody have a clue what I am doing wrong?
>
> Thanks,
>
> John

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: GORM problem with String primary key

Robert Fletcher
In reply to this post by pbwebguy
class RoleType {
       String id
       String description

       static constraints = {
                id(blank:false, nullable:false, maxLength:20)
                description(blank:true, nullable:true)
        }

       static mapping  = {
               version false
               id column: 'role_type_code', generator: 'assigned'
       }
}

You're mapping the id property of your object to the column named
role_type_code. There's no need for a 2nd property.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: GORM problem with String primary key

pbwebguy
Thanks!!!  That worked perfectly - so simple - bang my head on desk...
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: GORM problem with String primary key

Robert Fletcher
I'm not sure if there's a way to have the GORM DSL recognise a
different property as the identifier. The fact GORM classes get an
ident() method seems to suggest so but I can't find any reference to
how to declare it.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: GORM problem with String primary key

pbwebguy
In reply to this post by Robert Fletcher
I believe that I found a bug.  If I take the your example and simply add , sqlType:'char(10)' to it, GORM no longer honors the column name that was set.  It instead reverts to id.  This is what was flubbing me up before.

class RoleType {
        String id
        String description

        static constraints = {
                id(blank:false, nullable:false, maxLength:20)
                description(blank:true, nullable:true)
        }

        static mapping  = {
                version false
                id column: 'role_type_code', generator: 'assigned', sqlType: 'char(10)'
        }
}
Loading...