我尝试使用grails和groovy从postgreySQL基础上获取数据但我在访问列表foregin key id时遇到问题

时间:2017-09-05 07:01:01

标签: grails groovy

ProductViewerController.groovy

 class ProductViewerController {

    def index(Integer id) { 
  def result=ProductLoader.createCriteria().list {
   eq("product_barcode",id)
  }

  result.each {
   notes->println "${notes}"
  }
  render(view:'index.gsp')  
 }
}

ProductLoader.groovy(模型类)

 class ProductLoader {
 String store
 double price
 String notes
 static belongsTo =[product_barcode : Product]    
 static constraints = {
  store()
  price()
  notes()

    }
}

我试图在外键id的基础上获取数据然后得到classCastException:enter image description here

1 个答案:

答案 0 :(得分:1)

需要将属性product_barcode与对象类型Product

进行比较
def result = ProductLoader.createCriteria().list {
   eq("product_barcode", Product.get(id))
}

或者您加入外国表格,如

def result = ProductLoader.createCriteria().list {
   product_barcode {
       eq("id", id)
   }
}