简体   繁体   中英

ID lookup field in GORM in grails

I am new to Grails and GORM and I am trying to One to Many relationship but not with default id field. Here is my scenario:

Table structure in the database:

USERPROFILE

  • iduserprofile
  • username

ROLE

  • idrole
  • rolename

USER_ROLE

  • iduserprofile
  • idrole

Domains:

class Userprofile {

long iduserprofile
String username

    static mapping = {
    datasource 'ALL'
    id name: 'iduserprofile'
    version false
}

class Role {

long idrole;
String rolename;

static mapping = {
    datasource 'ALL'
    id name: 'idrole'
    version false
}

}

class UserRole {

Userprofile user
Role role

static mapping = {
    datasource 'ALL'
    version false
}

}

When I try to get the user or role object from UserRole domain, it is always looking for user_id or role_id in the USER_ROLE table. Why is it not looking for iduserprofile or idrole? How can i change the code to look for isuserprofile or idrole?

Thanks

GORM by convention will use/generate id as identifier for your domains. If you have legacy tables or just a desire to break convention, you'll need to specify your custom column names. For example for Role mapping, add the following:

static mapping = {
  datasource 'ALL'
  id name: 'idrole', column: 'idrole'
  version false
}

It seems to me that the easiest thing to do would be to copy your database and then change the names of the id fields if you have legacy tables. If not then just make life simple by conforming to convention.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM