我有兩個類:如何恢復`發生異常後不刷新會話'錯誤?
用戶
class User {
//relationships. . . .
static belongsTo = [ company : Company, role : Role ]
static hasMany = [ holidays : Holiday ]
String login
String password
static constraints = {
login(unique:true,size:6..15)
}
String toString() {
this.login
}
}
並有另一個類是這樣的:
角色:
class Role {
String roleName
String privilege
static hasMany = [ user : User ]
static constraints = {
privilege(nullable:true)
roleName(unique:true)
}
String toString() {
this.roleName
}
}
我寫了一個集成測試這樣的:
def user1 = new User(login:"aravinth", password:"secret")
def user2 = new User(login:"anto", password:"secret")
def user3 = new User(login:"antoa", password:"secret")
def role1 = new Role(roleName:"manager").save()
def role2 = new Role(roleName:"devleoper").save()
role1.addToUser(user1)
role1.addToUser(user2)
role2.addToUser(user3)
assert "manager" == user1.role.roleName
此測試正常工作。但是,當我這下面一行添加到我上面的測試代碼:
def roleMembers = Role.findByRoleName("manager")
我得到這樣的錯誤:
null id in mnm.schedule.User entry (don't flush the Session after an exception occurs)
org.hibernate.AssertionFailure: null id in mnm.schedule.User entry (don't flush the Session after an exception occurs)
at org.grails.datastore.gorm.GormStaticApi.methodMissing(GormStaticApi.groovy:108)
at mnm.schedule.RoleItntegrationTests.testAddingRolesToUser(RoleItntegrationTests.groovy:44)
回事請告訴我?我哪裏錯了?
我使用的是Grails 2.0。
在此先感謝。