使用BootStrap类将数据加载到数据库中,但只加载了部分数据

时间:2015-07-25 20:46:15

标签: grails

我是从BootStrap.groovy将数据加载到我的模型中但我无法弄清楚为什么只有第一组4(ListItPlan)才被加载到数据库(hql)。当我使用dbconsole查看表时,其他两个表为空(Plan,Cred)。它是否与加载数据的顺序或约束(我只在ListItPlan模型上有关)有关?

class BootStrap {

    def init = { servletContext ->
        new ListItPlan(m_id: "248656", plan_id: "12345XX9876543").save()
        new ListItPlan(m_id: "209459", plan_id: "12345XX9876543").save()
        new ListItPlan(m_id: "248656", plan_id: "56748XXX123933").save()
        new ListItPlan(m_id: "209459", plan_id: "56748XXX123933").save()

        new Plan(plan_id: "12345XX9876543", p_id_type: "PLAN-ID").save()
        new Plan(plan_id: "56748XXX123933", p_id_type: "PLAN-ID").save()

        new Cred(m_id: "248656", d_name: "Lorem Ipsum").save()
        new Cred(m_id: "209459", d_name: "Ipsum").save()
    }
    def destroy = {
    }
}

fyi:我在控制台中看不到任何错误。

所以,现在我遇到了pk约束问题。在做了一些阅读之后,我知道belongsTo和hasMany是罪魁祸首,但是在添加数据时我似乎无法使订单正确。我先创建Cred然后把它放在ListItPlan中吗?像这样......

def d1 = new Cred(m_id: "248656", d_name: "Lorem Ipsum").save(failOnError:     true)
def ldp1 = new ListItPlan(Cred : d1, plan_id: "12345XX9876543").save(failOnError: true)

这是我的模特......     ...     class ListItPlan {         字符串m_id         String plan_id

    String toString() {
        "${plan_id}"
    }
    static hasMany = [creds : Cred, plans : Plan]

    static constraints = {
        m_id()
        plan_id()
    }
}
…
class Cred {
    String m_id
    String g_name
    static belongsTo = [owner : ListItPlan]
    static hasMany = [plans : Plan]

    static constraints = {
    }
}
…
class Plan {
    String plan_id
    String plan_id_type
    static belongsTo = [listItPlan : ListItPlan, cred: Cred]

    static constraints = {  
    }
}

0 个答案:

没有答案