Abhilash Shajan
Abhilash Shajan

Reputation: 650

Object relational mapping in Grails

I have 2 tables named 'employees' and 'dataentry'. I need to set the employee_id column in employees table as primary key, and also employee_id column in dataentry as a foreign key of employees.employee_id. How can I do this in groovy domain class.

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)
		}

}

Please tell me how to make a mapping with cascaded

Upvotes: 0

Views: 78

Answers (1)

railsdog
railsdog

Reputation: 1501

Something akin to this should get the entities joined. Convention is to name an entity in the singular, not a plural (Employee, not Employees):

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

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

Let the system take care of the details.

Upvotes: 2

Related Questions