开发者

update unidirectional one to one problem

开发者 https://www.devze.com 2023-03-05 04:28 出处:网络
I have two domain classes: class Domain1 { String val11 String val12 Domain2 domain2 static constraints = {

I have two domain classes:

class Domain1 {

  String val11
  String val12
  Domain2 domain2

  static constraints = {
  }
}

class Domain1Controller{

  /**
   * Create new Domain1 entity instance
   */
  def create = {
    def domain1 = new Domain1()
    def domain2 = Domain2.get(params.domain2)
    if(domain2!=null){
      domain1.domain2 = domain2
    }

    domain1.properties=params
    domain1.save(flush: true)

    String strJson = (domain1 as JSON)
    render strJson
  }

  /**
   * Update Domain1 entity fields values
   */
  def update = {
    Domain1 domain1 = Domain1.findById(params.id)
    params.remove("id")
    if (domain1 != null) {
      domain1.properties=params
      domain1.save(flush:true)
      String strJson = (domain1 as JSON)
      render strJson
    }
  }
}

class Domain2 {

  String val21
  String val22

  static constraints = {
  }
}

class Domain2Controller{

  /**
   * Create new Domain2 entity instance
   */
  def create = {
    def domain2 = new Domain2()          
    domain2.properties=params
    domain2.save(flush:true)
    String strJson = (domain2 as JSON)
    render strJson
  }

  /**
   * Update Domain2 entity fields values
   */
  def update = {
    Domain2 domain2 = Domain2.findById(params.id)
    params.remove("id")
    if (domain2 != null) {
      domain2.properties=params
      domain2.save(flush: true)
      String strJson = (domain2 as JSON)
      render strJson
    }
  }
}

My problem is when I create asso开发者_Python百科ciated objects,I cannot update domain1.

I think reason maybe in save() method... maybe not

Is there anyone who know why I cannot update Domain1 properties ?

I use grails-1.3.2 and hbase-0.2.4 plugin.

P.S. hbase does not understand mapping..

Thanks for help.


Given the exception you provided in the comment, I think the problem is the line where You call domain1.properties=params. Domain properties map contain some specific keys, and when you assign params map to it, those specific (ie. class property here) are missing, so GORM cant access them.

Use bind() method to bind parameter values to your domain object this way:

    def domain1 = new Domain1()
    bind(domain1, params)
    def domain2 = Domain2.get(params.domain2)
    if(domain2!=null){
      domain1.domain2 = domain2
    }

    domain1.save(flush: true)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号