GORM save对象不能保存问题

在使用grails框架时save对象没有更新到数据库,框架也没有任何报错,一直以为是datasource配置文件出了问题,后来发现原来是save的对象保存时出了问题而没有反馈。

package hello

class Quote {

	String content
	String author
	Date created = new Date()

    static constraints = {
		//content(nullable:true)
		//author(nullable:true)
    }
}

 如上代码在grails框架启动时会建立数据库表,字段默认是not null。在插入对象时,如果未全部赋值给对象里的属性就会悄声无息的不插入数据(或者说插入无效)。


因此,每次建立domain对象后务必添加约束条件(nullable:true),这样在插入对象时就能保证完全插入到数据库。


grails不会主动说明哪个字段受到约束不能插入数据库,不过可以通过代码说明不能插入的问题

package hello

class QuoteController {

    def index() { 
		def q = new Quote(content:'aaa')
		if(!q.save()){
			q.errors.allErrors.each{
				println it
			}
		}

		render "ok"
	}
}
 

猜你喜欢

转载自akunamotata.iteye.com/blog/1742859