Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Grails-在刷新之前保存临时实例_Grails_Gorm - Fatal编程技术网

Grails-在刷新之前保存临时实例

Grails-在刷新之前保存临时实例,grails,gorm,Grails,Gorm,我正在Grails上编写一个应用程序。我正在尝试使用addTo方法将子数据库记录添加到父表中。我遵循关于addTo方法的文档。例如,文档中说创建父类: class Author { String name static hasMany = [fiction: Book, nonFiction: Book] } class Cafee { String cafeeName = "" int totalReservationPlaces = 0 double

我正在Grails上编写一个应用程序。我正在尝试使用addTo方法将子数据库记录添加到父表中。我遵循关于addTo方法的文档。例如,文档中说创建父类:

class Author {    String name
    static hasMany = [fiction: Book, nonFiction: Book]
}
class Cafee {

    String cafeeName = ""
    int totalReservationPlaces = 0
    double placeCost = 0
    String currencyType = ""
    boolean isReservationAvailable = false
    boolean reservationTimeLimit = false
    boolean reservationDateLimit = false
    int totalPlaces = 0
    long startTimeLimit = 0
    long endTimeLimit = 0
    Date startDateLimit = new Date()
    Date endDateLimit = new Date()  

    static constraints = {
        cafeeName blank: false, unique: true
    }

    String getCafeeName(){
        return cafeeName
    }

    static hasMany = [admin: Person]
}
按照此操作,我创建了我的父类:

class Author {    String name
    static hasMany = [fiction: Book, nonFiction: Book]
}
class Cafee {

    String cafeeName = ""
    int totalReservationPlaces = 0
    double placeCost = 0
    String currencyType = ""
    boolean isReservationAvailable = false
    boolean reservationTimeLimit = false
    boolean reservationDateLimit = false
    int totalPlaces = 0
    long startTimeLimit = 0
    long endTimeLimit = 0
    Date startDateLimit = new Date()
    Date endDateLimit = new Date()  

    static constraints = {
        cafeeName blank: false, unique: true
    }

    String getCafeeName(){
        return cafeeName
    }

    static hasMany = [admin: Person]
}
文档说明创建子类:

class Book {    String title
    static belongsTo = [author: Author]
}
class Person {

    transient springSecurityService

    String username
    String password
    boolean enabled = true
    boolean accountExpired
    boolean accountLocked
    boolean passwordExpired

    String firstName
    String lastName
    String email
    String inn = ""
    boolean isAdminCafee = false
    static transients = ['springSecurityService']

    static belongsTo = [cafee:Cafee]

    static constraints = {
        username blank: false, unique: true
        firstName blank: false
        lastName blank: false
        password blank: false
        email blank: false, unique: true
    }

    static mapping = {
        password column: '`password`'
    }

    Set<Authority> getAuthorities() {
        PersonAuthority.findAllByPerson(this).collect { it.authority }
    }

    def beforeInsert() {
        encodePassword()
    }

    def beforeUpdate() {
        if (isDirty('password')) {
            encodePassword()
        }
    }

    protected void encodePassword() {
        password = springSecurityService?.passwordEncoder ? springSecurityService.encodePassword(password) : password
    }
}
遵循以下步骤,我创建了我的子类:

class Book {    String title
    static belongsTo = [author: Author]
}
class Person {

    transient springSecurityService

    String username
    String password
    boolean enabled = true
    boolean accountExpired
    boolean accountLocked
    boolean passwordExpired

    String firstName
    String lastName
    String email
    String inn = ""
    boolean isAdminCafee = false
    static transients = ['springSecurityService']

    static belongsTo = [cafee:Cafee]

    static constraints = {
        username blank: false, unique: true
        firstName blank: false
        lastName blank: false
        password blank: false
        email blank: false, unique: true
    }

    static mapping = {
        password column: '`password`'
    }

    Set<Authority> getAuthorities() {
        PersonAuthority.findAllByPerson(this).collect { it.authority }
    }

    def beforeInsert() {
        encodePassword()
    }

    def beforeUpdate() {
        if (isDirty('password')) {
            encodePassword()
        }
    }

    protected void encodePassword() {
        password = springSecurityService?.passwordEncoder ? springSecurityService.encodePassword(password) : password
    }
}
在Bootstrap中遵循它我已经做到了:

def user = Person.findOrSaveWhere(username: 'testerAndrewRes', password:'password', firstName:'Andrew', lastName:'Bobkov', email:'pragm@gmail.com', isAdminCafee: true,
             inn: '1234567890')
         println user
         if(!user.authorities.contains(adminRole))
         {
             PersonAuthority.create(user, adminRole, true)
         }
         def newCafe = new Cafee(cafeeName: "Tarelka").addToAdmin(user).save()
但我有一个错误:

ERROR context.GrailsContextLoaderListener  - Error initializing the application: object references an unsaved transient instance - save the transient instance before flushing: restorator.auth.Person; nested exception is org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: restorator.auth.Person
Message: object references an unsaved transient instance - save the transient instance before flushing: restorator.auth.Person; nested exception is org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: restorator.auth.Person

我做错了什么?

我想错误信息很清楚。
尝试添加:

user.save(flush:true)
之前:

def newCafe = new Cafee(cafeeName: "Tarelka").addToAdmin(user).save()

PS.println用户打印:restorator.auth.Person:(未保存)。为什么?因为findorsavehere必须始终引用已保存的实例。你可以先做addToAdmin然后再做new Person来测试一下。。作为测试。。使用findOrSave,您不会刷新,因此不知道是否有其他逻辑导致您的问题,在增强到其他方法之前,我会做一个简单的测试,而不是按照docSolved,show:[Grails,GORM,relationship.Optional child records][1]:而不是user.save flush:true尝试user.save flush false我认为这在事务{}或事务服务方法的事务设置中没有帮助。