[英]Grails syntax for joining tables on composite foreign key
Could anyone please tell me how to join tables in Grails? 谁能告诉我如何在Grails中加入表格? I need help with the syntax.
我需要语法方面的帮助。
Let's say I have three tables and they don't have any explicitly defined foreign key constraints: 假设我有三个表,并且它们没有任何明确定义的外键约束:
EMPLOYEE:
empid
name
emp_deptid
emp_teamid
title
salary
hiredate
DEPT:
dept_deptid
deptname
location
size
numOfTeams
TEAM:
team_teamid
teamname
team_deptid
responsibility
size
I want to join EMPLOYEE and TEAM on TEAMID and DEPTID. 我想在TEAMID和DEPTID上加入EMPLOYEE和TEAM。 I know how to join tables in a SQL query.
我知道如何在SQL查询中连接表。 I would actually like to know how to do the table joins in Grails(on these selected columns using OnetoOne, OnetoMany, hasMapped, etc.).
我真的想知道如何在Grails中进行表连接(在这些选定的列上使用OnetoOne,OnetoMany,hasMapped等)。 thanks
谢谢
edit: 编辑:
Class Emp(){
String empid
String teamid
String deptid
.....
.....
Date hiredate
Team team
static mapping = {
....
....
deptid column:'emp_deptid'
teamid column:'emp_teamid'
.....
.....
team column: ['teamid', 'deptid']
}
}
Class Team(){
...
...
String teamid
String deptid
static mapping ={
...
deptid column:'team_deptid'
teamid column:'team_teamid'
....
}
}
There are 2 subsections in GORM doc section 5.4.1 and 5.4.2 , both named "Querying Associations". GORM doc 5.4.1和5.4.2中有2 个小节 ,两个小节都命名为“查询关联”。
edit: 编辑:
Suddenly, it's not that easy - sorry for being inattentive. 突然间,这并不容易 - 抱歉不专心。
Grails officially supports syntax for only compound primary keys , not foreign. Grails官方支持仅复合主键的语法 ,而不支持外语。 I found a bunch of issues about composite foreign keys in Grails: GRAILS-4606 and GRAILS-4256 .
我在Grails中发现了一堆关于复合外键的问题: GRAILS-4606和GRAILS-4256 。
I can propose approaches that might work, but I didn't test this myself. 我可以提出可行的方法,但我自己没有测试过。
A "list of columns" syntax (sample taken from GRAILS-4256): “列列表”语法(样本取自GRAILS-4256):
class Employee {
Team team
static mapping = {
// ... column mapping
team column: [team_id, dept_id]
}
}
Lauro Becker used custom configuration class that adds composite foreign key mapping syntax - this looks to have worked for him. Lauro Becker使用自定义配置类来添加复合外键映射语法 - 这看起来对他有用。
Some people even create updatable database views with a single key field, and map domain to them, not to original tables - but I believe that's an overkill. 有些人甚至用一个关键字段创建可更新的数据库视图,并将域映射到它们,而不是原始表 - 但我认为这是一种矫枉过正。
There are lots of ways to query and join in grails. 有很多方法可以查询和加入grails。 Here's one example of a simple way to do a SQL join of two tables:
这是一个简单的方法来实现两个表的SQL连接的一个示例:
class Dept {
...
}
class Team {
...
Dept dept
}
Team.list(fetch: [dept: 'eager'])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.