简体   繁体   English

Grails Map域类的列名问题

[英]Grails Map domain class column name problems

I have a MS SQL DB (which I can't change) with 3 tables : 我有一个MS SQL DB(无法更改),其中包含3个表:

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

In Grails I have 3 domain classes : 在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" 
    }
}

I'm trying to create a list using :- 我正在尝试使用:-创建列表

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

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

And it is throwing an error 它抛出一个错误

Invalid column name 'cgroup_id'.. Stacktrace follows: 无效的列名“ cgroup_id”。堆栈跟踪如下:

As you can see, I'm trying to map the correct column name (in the Contact_group domain class) as the table has been renamed from group to c_group in the DB, and just to complicate matters for some reason I decided to call the domain class Cgroup in my Grails app. 如您所见,由于表已在数据库中从group重命名为c_group ,因此我试图映射正确的列名(在Contact_group域类中),并且由于某种原因而使事情复杂化,我决定调用该域我的Grails应用中的Cgroup类。 (I can change this if needs be) (如果需要,我可以更改此设置)

So, I'm now in a bit of a muddle about how best to create the list. 因此,我现在对如何最好地创建列表有些困惑。 Any suggestions or pointers? 有什么建议或指示吗?

In your mapping you should point to the class property ( cgroup_id should be cgroup , since that is what you named it in your domain.) 在映射中,您应指向类属性( cgroup_id应为cgroup ,因为这是您在域中命名的名称。)

Also, your column name in your table is id not c_group_id so you should set the column as id . 另外,表中的列名不是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