Grails中的对象关系映射

时间:2016-12-08 09:07:00

标签: grails gorm

  

我有两个名为'employees'和'dataentry'的表。我需要将employees表中的employee_id列设置为主键,并将dataentry中的employee_id列设置为employees.employee_id的外键。我怎么能在groovy域类中这样做。

here is my employees.groovy model class

package com.standout.utilityapplication

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

class Employees {

static mapping = {
	    table 'employees'
		version false
   }

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

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

}

and here is my dataentry.groovy model class

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

class Dataentry {

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

}

  

请告诉我如何使用级联

进行映射

1 个答案:

答案 0 :(得分:2)

类似于此的东西应该让实体加入。惯例是以单数形式命名实体,而不是复数(雇员,而不是雇员):

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

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

让系统处理细节。