在Grails中映射两个域类 [英] Mapping two domain classes in Grails

查看:118
本文介绍了在Grails中映射两个域类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要映射数据库中的两个表。第一个是学生表。它看起来像这样:

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

专业表如下所示:

id
description

我需要映射学生的主要代码,其中 major_code_1 major_code_2 指向<$主表中的c $ c> id 。我怎么能这样做?谢谢!

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!

推荐答案

这是一个映射到您的模式的简单模型:

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

我忽略了所有 belongsTo 和其他所有权字段,因为您没有在您的问题中指定级联行为。

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

这篇关于在Grails中映射两个域类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆