2016-12-08 204 views
0

我有2個名爲'employees'和'dataentry'的表。我需要將employees表中的employee_id列設置爲主鍵,並將dataentry中的employee_id列設置爲employees.employee_id的外鍵。我如何在groovy域類中做到這一點。Grails中的對象關係映射

here is my employees.groovy model class 

package com.standout.utilityapplication 
 

 
import java.util.Date; 
 
import com.standout.utilityapplication.Dataentry 
 

 
class Employees { 
 

 
static mapping = { 
 
\t  table 'employees' 
 
\t \t version false 
 
    } 
 

 
     static hasMany = [employee_id:Dataentry] 
 
\t \t String employee_id 
 
\t \t String employee_name 
 
\t \t String team 
 
\t \t Long contact_no 
 
\t \t String designation 
 

 
\t \t 
 
\t \t 
 
\t \t static constraints = { 
 
\t \t \t 
 
\t \t \t employee_id(nullable: false,maxSize:10) 
 
\t \t \t employee_name(nullable: false,maxSize:100) 
 
\t \t \t team(nullable: true,maxSize:40) 
 
\t \t \t contact_no(nullable: true,maxSize:10) 
 
\t \t \t designation(nullable: true,maxSize:40) 
 
\t \t \t 
 
\t \t } 
 

 
}

and here is my dataentry.groovy model class 

package com.standout.utilityapplication 
 
import com.standout.utilityapplication.Employees 
 

 
class Dataentry { 
 

 
\t static mapping = { 
 
\t \t table 'dataentry' 
 
\t \t version false 
 
    } 
 
\t  static belongsTo = [employee_id:Employees] 
 
\t \t String employee_id 
 
\t \t String team 
 
\t \t Date receipt_dt 
 
\t \t String restaurant_name 
 
\t \t int number_of_persons 
 
\t \t float amount 
 
\t \t Date bill_submitted_dt 
 
\t \t String reimbursed 
 
\t \t char presented_bank_fl 
 
\t \t Date presented_bank_dt 
 
\t \t String create_id 
 
\t \t Date create_dt 
 
\t \t String mod_id 
 
\t \t Date mod_dt 
 
\t \t 
 
\t \t 
 
\t \t static constraints = { 
 
\t \t \t reimbursed(nullable: true) 
 
\t \t \t presented_bank_fl(nullable: true) 
 
\t \t \t presented_bank_dt(nullable: true) 
 
\t \t \t mod_id(nullable: true) 
 
\t \t \t mod_dt(nullable: true) 
 
\t \t } 
 

 
}

請告訴我如何使級聯映射

回答

2

類似這樣的東西應該可以讓實體加入。公約是命名爲單數,不是複數的實體(員工,不是僱員):

class Employee { 
    static hasMany = [dataentries:Dataentry] 
    static mapping = { 
     id name:'employee_id' 
    } 
} 

class Dataentry { 
    static belongsTo = [employee:Employee] 
} 

讓系統採取的細節。