【问题标题】:grails won't save but have no errorgrails 不会保存但没有错误
【发布时间】:2010-11-30 05:31:56
【问题描述】:

您好,我有一个如此简单的域,如下所示

// page 52 Bankruptcy Questions 
class FamilyLawFinancial{

    Date dateOfTheOrder ;
    boolean isPartyToFamilyLawProperty = false; 
    boolean isCurrentlyInvolvedInFamilyLawProperty = false;
    boolean isBecomeInvolvedInProceedings = false;

    static belongsTo=[person:Person];

    static constraints = {
        dateOfTheOrder(nullable:true);
        isPartyToFamilyLawProperty(nullable:false);
        isCurrentlyInvolvedInFamilyLawProperty(nullable:false);
        isBecomeInvolvedInProceedings(nullable:false);
    }

    String toString(){
        "${id}"
    }
}

这是保存数据的控制器:

    def save = {
    def person = Person.get(session.curperson.id);
    def obj = FamilyLawFinancial.findByPerson(person);
    if(obj == null){
        obj = new FamilyLawFinancial();
        obj.person = person ; 
    }
    params.dateOfTheOrder = myutil.formatDate(params.dateOfTheOrder);
    obj.properties = params;

    println(obj.hasErrors());
    println(obj.dateOfTheOrder);
    if(obj.hasErrors() && !obj.save(flush:true)){
        println("errors: ${obj.errors}");
        flash.message = "error found";
        println("save familyLawFinancial errors: ${errors}");
    }else{
        flash.message = "saved ";
    }
    redirect(controller:'frontPage', action:'index'); return ;
}

obj.hasErrors() 产生 false(这意味着没有错误)但它不会保存到数据库中。知道如何调试吗?

ps: myutil.formatDate() --> 用于将日期字符串如 19/11/2010 转换为 Date()

【问题讨论】:

    标签: java grails jakarta-ee grails-domain-class


    【解决方案1】:
    if(obj.hasErrors() && !obj.save(flush:true)){
    

    如果&& 之前的条件计算为false,则不会计算&& 之后的条件。

    由于 false && true 的计算结果为 false,从语言的角度来看,计算第二个条件的效率会很低。

    毕竟,在这种情况下,obj.save(..) 永远不会被调用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-05
      • 1970-01-01
      • 2011-12-22
      • 1970-01-01
      • 1970-01-01
      • 2015-01-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多