Quantcast

customize mapping to lookup table code column (not id)?

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

customize mapping to lookup table code column (not id)?

yuanjing
When I have a lookup table, by default, it maps to the id column, which is not ideal most of the time.
How can I change the mappedBy to code column? Thanks.

class Book {
    String title
    MyLookup mylookup
}

class MyLookup {
    String code
    String name
}
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: customize mapping to lookup table code column (not id)?

Robert Fischer
You almost certainly don't want to do this.  At least not if "MyLookup" is anything but a read-only
table.  It will lead you to tears and recriminations when your assumptions about uniqueness are
violated, and can make your life somewhat tricky (see the explicit-insert plugin: you'll want it).

That said, see here:
http://grails.org/doc/1.1.x/guide/5.%20Object%20Relational%20Mapping%20(GORM).html#5.5.2.4%20Custom%20Database%20Identity

~~ Robert Fischer, Smokejumper IT Consulting.
Enfranchised Mind Blog http://EnfranchisedMind.com/blog

Grails Expert Retainer Services
http://smokejumperit.com/grails-retainer/


yuanjing wrote:

> When I have a lookup table, by default, it maps to the id column, which is
> not ideal most of the time.
> How can I change the mappedBy to code column? Thanks.
>
> class Book {
>     String title
>     MyLookup mylookup
> }
>
> class MyLookup {
>     String code
>     String name
> }

---------------------------------------------------------------------
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
star

Re: customize mapping to lookup table code column (not id)?

yuanjing
Yes, the lookup tables are read only, and uniqueness is ensured:
class Book {
    String title
    MyLookup mylookup
}

class MyLookup {
    String code
    String name
    static constraints = {
        code(unique:true, blank:false)
    }
}

The link you gave does not do what I want. I did not mean to change how the id is generated.

I want to have "code" as foreign key. How?
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: customize mapping to lookup table code column (not id)?

burtbeckwith
Make 'code' the primary key (this is a requirement of most databases):

   class MyLookup {
      String id
      String name

      void setCode(String code) { id = code }
      String getCode() { id }

      static transients = ['code']

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

The code<->id translation is required since Grails expects the pk to be named 'id'. Create instances like you would with the earlier version, e.g.

   def lookup = new MyLookup(code: 'foo', name: 'bar').save()

Burt

On Saturday 24 October 2009 10:59:38 pm yuanjing wrote:

>
> Yes, the lookup tables are read only, and uniqueness is ensured:
> class Book {
>     String title
>     MyLookup mylookup
> }
>
> class MyLookup {
>     String code
>     String name
>     static constraints = {
>         code(unique:true, blank:false)
>     }
> }
>
> The link you gave does not do what I want. I did not mean to change how the
> id is generated.
>
> I want to have "code" as foreign key. How?

signature.asc (204 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: customize mapping to lookup table code column (not id)?

yuanjing
Thanks, Burt ! This is exactly I was looking for.

Burt Beckwith wrote
Make 'code' the primary key (this is a requirement of most databases):

   class MyLookup {
      String id
      String name

      void setCode(String code) { id = code }
      String getCode() { id }

      static transients = ['code']

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

The code<->id translation is required since Grails expects the pk to be named 'id'. Create instances like you would with the earlier version, e.g.

   def lookup = new MyLookup(code: 'foo', name: 'bar').save()

Burt
 
Loading...