0

I have two tables in our database that need mapped. The first is a Student table. It looks something like this:

id
first_name
last_name
major_code_1
major_code_2

And the Major table is like this:

id
description

I need to map the major codes of the student, where major_code_1 and major_code_2 point to an id in the Major table. How could I do this? Thanks!

2 Answers 2

3

Here is a simple model which maps to your schema:

class Student {
    String firstName
    String lastName
    Major firstMajor
    Major secondMajor

    static mapping = {
        table 'Student'
        firstMajor column: 'major_code_1'
        secondMajor column: 'major_code_2'
    }
}

class Major {
    String description

    static mapping = {
        table 'Major'
    }
}

I've left out all belongsTo and other ownership fields as you didn't specify cascade behavior in your question.

Sign up to request clarification or add additional context in comments.

Comments

0

The code can look like (with one-to-many relations: Major can have many Students):

class Student{

    Long id 
    String first_name
    String last_name

    static belongsTo = [
            major_code_1: Major
            , major_code_2: Major
    ]

    static mapping = {
        table 'Student'
    }

}


class Major{

    Long id 
    String description

    static hasMany = [
        student_1: Student
        , student_2: Student
    ]

    static mappedBy = [
            student_1: 'major_code_1'
            , student_2: 'major_code_2'
    ]

    static mapping = {
        table 'Major'
    }

}

But could you please explain me the idea of this two tables. Because it looks like recursive relationship many to many between Major Entity which is called Student. I wonder if you shouldn't have student_code_1 and student_code_2 in the Major table.

-------------------------------- EDIT ------------------------------------------------

With many-to-one relation (Many Students have the same Major)

class Student{

    Long id 
    String first_name
    String last_name

    Major major_code_1
    Major major_code_2

    static mapping = {
        table 'Student'
    }

}


class Major{

    Long id 
    String description

    static belongsTo = [
        student_1: Student
        , student_2: Student
    ]

    static mappedBy = [
            student_1: 'major_code_1'
            , student_2: 'major_code_2'
    ]

    static mapping = {
        table 'Major'
    }

}

5 Comments

Basically the Student object can have two majors...major1, major2. In the database, the the major_code_1 and major_code_2 in the student able is just a code that points to the Major table. From there you can get the major description (Computer Information Science, Economics, etc.) Being a university, this is a legacy database so if it doesn't map well to Grails domain classes then well, that's why.
Now I understand. So code from my answer should work fine. You can only add a constraint is Student class to avoid having two the same majors for one student.
Would this need to change a bit if the Student is on the owning side?
I've added solution with many-to-one relation. So now the Student is on the owning side.
The first approach if you drop a major all your students will be gone and with the second approach if you drop a Student the corresponding major will be gone.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.