我有两个域类,有很多映射。
一个类:
class Line {
static belongsTo = [header : Header]
Long invoiceId
Long lineNumber
Long oneId
Long secondId
Long thirdId
static mapping = {
table('LINES_2_V')
version(false)
id composite: ['invoiceId', 'lineNumber']
}
}
另一个类:
class Header {
static hasMany = [lines: Line]
Long id
static mapping = {
table('HEADERS_2_V')
version(false)
id column:'INVOICE_ID'
}
}
创建:
Header.createCriteria().list() {
createAlias('Lines', 'l', CriteriaSpecification.INNER_JOIN)
if (oneId) {
eq('l.oneId', oneId)
} else {
isNull('l.oneId')
}
if (secondId) {
eq('l.secondId', secondId)
}else {
isNull('l.secondId')
}
if (thirdId) {
eq('l.thirdId', thirdId)
}else {
isNull('l.thirdId')
}
}
我收到以下错误:
ORA-00904: "L1_"."HEADER_ID": invalid identifier
Hibernate 生成的选择如下所示:
Hibernate: select * from ( select this_.INVOICE_ID as INVOICE1_0_1_
from HEADERS_2_V this_,
LINES_2_V ihl1_ where this_.INVOICE_ID=l1_.header_id and ihl1_.project_id is null
and ihl1_.transaction_id is null and ihl1_.po_header_id is null ) where rownum <= ?
为什么它尝试通过 HEADER_ID
进行映射?如何使其通过 INVOICE_ID
进行映射?
最佳答案
因为这是对存储在行表中的 header 的反向引用。你的表格是这样的:
Lines_2_V : invoice_id, line_number, header_id, one_id, second_id ,thid_id
Header_2_V: invoice_id
由于 Gorm 已经将 Header 的 id
存储在 Line 表中,因此它使用它来连接它们。
当您将 belongTo
作为 map 时:
static belongsTo = [header : Header]
Grails/Gorm 将在您的表中创建反向引用。您还可以使用
static belongsTo = Header
哪个gorm创建中间表来连接它们 有关更多信息,请参阅 Peter Ledbrook 撰写的 GORM 陷阱文章
顺便说一句,行
应该是行
:
createAlias('Lines', 'l', CriteriaSpecification.INNER_JOIN)
应该是
createAlias('lines', 'l', CriteriaSpecification.INNER_JOIN)
关于Grails 中定义的映射属性有很多关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20396377/