参考:The Grails Framework - Reference Documentation 网址:http://itrc.jju.edu.cn/1.1/guide/single.html#7.1%20Declaring%20Constraints
1 启动IntelliJ IDEA时候,如果出现:
SEVERE: Problems copying method. Incompatible JVM?
java.lang.reflect.InvocationTargetException
at sun.reflect.GeneratedMethodAccessor98.invoke(Unknown Source),则是由于Grails 不支持 JDK 1.8 版本。修改JDK为1.7即可。
2 CSVN在Windows上执行部署
httpd.exe -k install -n "CollabNet Subversion Server" -f "target\dist\data\conf\httpd.conf",其中找到httpd.exe,可以对其target\dist\data\conf\httpd.conf目录根据路径进行修改。
target\dist\bin\httpd.exe -k install -n "CollabNet Subversion Server" -f "data\conf\httpd.conf"
如果该服务运行失败,打开cmd,输入sc delete CollabNetSubversionServer 删除该服务,可以去services.msc进行查看。
3 withTransaction 用来处理事务
def transferFunds = { Account.withTransaction { status -> def source = Account.get(params.from) def dest = Account.get(params.to)5 Many-to-Manydef amount = params.amount.toInteger() if(source.active) { source.balance -= amount if(dest.active) { dest.amount += amount } else { status.setRollbackOnly() } }
}
}
如果目的账户没有处于活动状态,系统将回滚事务,同时如果有任何异常抛出在事务的处理过程中也将会自动回滚。
4 constraints验证或者约束
class User { String login String password String email Integer agestatic constraints = {
login(size:5..15, blank:false, unique:true) //
login
属性必须在5-15个字符长度之间,不能为空,并且必须是唯一的password(size:5..15, blank:false)
email(email:true, blank:false) age(min:18, nullable:false) } }
class Book { static belongsTo = Author static hasMany = [authors:Author] String title } class Author { static hasMany = [books:Book] String name }
Grails在数据库层使用一个连接表来映射many-to-many,在这种情况下,Author
负责持久化关联,并且是唯一可以级联保存另一端的一方 。
例如,下面这个可以进行正常级联保存工作:
new Author(name:"Stephen King") .addToBooks(new Book(title:"The Stand")) .addToBooks(new Book(title:"The Shining")) .save()
而下面这个只保存 Book
而不保存 authors!
new Book(name:"Groovy in Action") .addToAuthors(new Author(name:"Dierk Koenig")) .addToAuthors(new Author(name:"Guillaume Laforge")) .save()