繁体   English   中英

Grails Map域类的列名问题

[英]Grails Map domain class column name problems

我有一个MS SQL DB(无法更改),其中包含3个表:

contact  
id, name, number
c_group  
id,name,email
contact_group  
id, contact_id, group_id  

在Grails中,我有3个域类:

class Cgroup {  
    String name
    String email

    static constraints = {
    }

    static mapping = {
        table name: "c_group"
        version false
    }   
}
class Contact {
    String name
    String number

    static constraints = {
        name nullable:false
    }

    static mapping = {
        version false
    }
}
class Contact_group {
    Cgroup cgroup
    Contact contact

    static constraints = {
    }

    static mapping = {
        version false
        cgroup_id column: "c_group_id", sqlType: "int" 
    }
}

我正在尝试使用:-创建列表

def contactInstance = Contact.get(id)  //I have previously got the id

List userGroupList = Contact_group.createCriteria().list(params) {            
    eq("contact", contactInstance)
}

它抛出一个错误

无效的列名“ cgroup_id”。堆栈跟踪如下:

如您所见,由于表已在数据库中从group重命名为c_group ,因此我试图映射正确的列名(在Contact_group域类中),并且由于某种原因而使事情复杂化,我决定调用该域我的Grails应用中的Cgroup类。 (如果需要,我可以更改此设置)

因此,我现在对如何最好地创建列表有些困惑。 有什么建议或指示吗?

在映射中,您应指向类属性( cgroup_id应为cgroup ,因为这是您在域中命名的名称。)

另外,表中的列名不是id c_group_id因此您应将列设置为id

static mapping = {
    version false
    table name: "contact_group"
    cgroup column: "id", sqlType: "int"        
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM