如何在grails中调用联接。这是我的Domain类:
package com.org.domain
class CoverageList {
Integer id
Integer pub_cat_id
Integer brand_id
Integer brand_set_no
String cov_list_desc
Date edit_date
Date part_edit_date
Integer category_id
static constraints = {
}
static mapping = {
table 'coverage_list'
version false
id column : 'COV_LIST_ID'
pub_cat_id column : 'PUB_CATEGORY_ID'
brand_id column : 'BRAND_ID'
brand_set_no column : 'BRAND_SET_NO'
cov_list_desc column : 'COV_LIST_DESC'
edit_date column : 'EDIT_DATE'
part_edit_date column : 'PART_EDIT_DATE'
category_id column : 'CATEGORY_ID'
}
}
package com.org.domain
class AssocCovList {
Integer id
Integer association_id
Integer cov_list_id
String assoc_line_code
Date edit_date
Date add_date
static constraints = {
}
static mapping = {
table 'assoc_cov_list'
version false
id column : 'ASSOC_COV_LIST_ID'
association_id column : 'ASSOCIATION_ID'
cov_list_id column : 'COV_LIST_ID'
assoc_line_code column : 'ASSOC_LINE_COD'
edit_date column : 'EDIT_DATE'
add_date column : 'ADD_DATE'
}
}
class Brand {
Integer id
String brand_name
String aaia_brand_id
String brand_owner_name
String parent_company_name
String site_link_code
Date edit_date
static hasMany = [cov_lists : CoverageList]
static constraints = {
}
static mapping = {
table 'brand'
version false
id column : 'BRAND_ID'
brand_name column : 'BRAND_NAM'
aaia_brand_id column : 'AAIA_BRAND_ID'
brand_owner_name column : 'BRAND_OWNER_NAME'
parent_company_name column : 'PARENT_COMPANY_NAME'
site_link_code column : 'SITE_LINK_COD'
edit_date column : 'EDIT_DATE'
}
}
def cov_list = CoverageList.executeQuery
("SELECT c.id, c.cov_list_desc, b.brand_name, a.id, a.association_id, a.cov_list_id FROM CoverageList c LEFT JOIN AssocCovList a ON (c.id = a.cov_list_id AND a.association_id = 1) JOIN Brand b ON b.id= c.brand_id ORDER BY b.brand_name, c.cov_list_desc")
但是我得到这样的错误:
{ERROR} portal-web.docroot.html.portal.render_portlet.jsp org.hibernate.hql.ast.QuerySyntaxException:
unexpected token: ON near line 1, column 187 [SELECT c.id, c.cov_list_desc, b.brand_name, a.id, a.association_id, a.cov_list_id, a.assoc_line_code, a.edit_date, a.add_date FROM com.org.domain.CoverageList c LEFT JOIN AssocCovList a ON (c.id = a.cov_list_id AND a.association_id = 1) JOIN Brand b ON b.id= c.brand_id ORDER BY b.brand_name, c.cov_list_desc]
我出错了,请任何人帮助我。
感谢
答案 0 :(得分:0)
您不能将LEFT JOIN用于2列。 按以下方式重写您的查询: ... CoverageList c LEFT JOIN AssocCovList a ON(c.id = a.cov_list_id)WHERE a.association_id = 1
答案 1 :(得分:0)
你最后的JOIN不应该是WHERE吗? 我会在FROM子句中添加“Brand b”,并用“WHERE b.id = c.brand_id”替换最新的JOIN。
我没有测试......这只是一个想法...
答案 2 :(得分:0)
您的代码看起来像Oracle SQL,但Grails使用Hibernate进行ORM。 Hibernate使用的Hibernate查询语言(HQL)似乎不支持连接后跟和ON子句。 HQL: The Hibernate Query Language