【问题标题】:Object relational mapping in GrailsGrails 中的对象关系映射
【发布时间】:2016-12-08 09:07:00
【问题描述】:

我有 2 个名为“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)
		}

}

请告诉我如何使用级联进行映射

【问题讨论】:

    标签: grails grails-orm


    【解决方案1】:

    类似的东西应该让实体加入。约定是以单数命名实体,而不是复数(Employee,不是Employees):

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

    让系统处理细节。

    【讨论】:

      猜你喜欢
      • 2011-04-24
      • 1970-01-01
      • 1970-01-01
      • 2013-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多