Not-null属性引用瞬态值 - 必须在当前操作之前保存瞬态实例

时间:2016-12-28 19:39:59

标签: hibernate grails gorm

我有以下域名

class User {

    Boolean accountLocked
    String password
    Boolean passwordExpired
    Boolean enabled
    String username
    String name
    String surname
    String gender
    String urlPicture
    String cellphone
    String address
    Integer pc
    String permission
    String email
    Date created
    Boolean accountExpired

    static hasMany = [
        customerOnlines: CustomerOnline,
        partners: Partner,
        roles: Role,
        salesmans: Salesman,
        subsidiaries: Subsidiary
    ]

    static mapping = {
        tablePerHierarchy false
        id generator: 'identity'
        table '`user`'
        password column: '`password`'
        email unique: true
    }

    static constraints = {
        accountLocked nullable: true, default: false
        password nullable: true
        passwordExpired nullable: true, default: false
        enabled nullable: true, default: true
        username nullable: true, maxSize: 45
        name nullable: true, maxSize: 45
        surname nullable: true, maxSize: 100
        gender nullable: true, maxSize: 15
        urlPicture nullable: true
        cellphone nullable: true, maxSize: 50
        address nullable: true
        pc nullable: true
        permission nullable: true
        created nullable: true
        accountExpired nullable: true, default: false
    }

    class Subsidiary {

        String subsidiaryName
        Date dob
        User user

        static hasMany = [
            partners: Partner,
            phones: Phone,
            salesmans: Salesman
        ]
        static belongsTo = [Partner, User]

        static mapping = {
            id generator: "identity"
            version false
        }

        static constraints = {
            subsidiaryName nullable: true, maxSize: 100
            dob nullable: true
        }

问题在于尝试创建新的子公司

    def save(Subsidiary subsidiary) {

    System.out.println(params)
    def usuario = new User(name:params.name,surname:params.surname,address:params.address,cellphone:params.cellphone,gender:params.gender,username:params.username,password:params.password,pc:params.pc)
    usuario.save flush:true
    def subsidiaria= new Subsidiary(name:params.namesub,user:usuario)


    subsidiaria.save flush:true



    request.withFormat {
        form multipartForm {
            flash.message = message(code: 'default.created.message', args: [message(code: 'subsidiary.label', default: 'Subsidiary'), subsidiary.id])
            redirect view: 'create'
        }
        '*' { respond subsidiary, [status: CREATED] }
    }
}

我得到错误not-null属性引用subsidiaria.save flush:true上的空值或瞬态值但是我无法找到问题,即使我使用usuario.addToSubsidiaries(subsidiaria).save()它仍然无效所有和 不断给我同样的错误。

1 个答案:

答案 0 :(得分:0)

我发现问题出在控制器本身,不幸的是我不得不把这两个表的所有参数都放在这样

     user.accountLocked=params.accountLocked
     user.password=params.password
     user.passwordExpired=params.passwordExpired
     user.enabled=params.enabled
     user.username=params.username
     user.name=params.name
     user.surname=params.surname
     user.gender=params.gender
     user.urlPicture=params.urlPicture
     user.cellphone=params.cellphone
     user.address=params.address
     user.pc=params.pc
     user.permission=params.permission
     user.email=params.email
     user.created=params.created
     user.accountExpired=params.accountExpired

我立即保存了

     user.save flush:true
     user.addToSubsidiaries(subsidiary)
     user.save flush:true

之后,两个表都使用提供的新信息进行了更新,看起来grails无法同时处理2个不同的bean。