简体   繁体   中英

Grails Domain class association with stored procedure resultset

Hi All,

Is it possible to associate the resultset of a stored procedure to grails domain class...?

I got this database view mapped to an domain class, which works like a charm, but in a typical functionality i need to generate db view based on certain parameters given by the end user. In that case i cant pass parameters to db view, so i created a stored procedure which would give me the same resultset as view but after calculations involving multiple table & getting parameters from end user.

In grails views (list.gsp) most of the plugin that i use is based on domain class , for ex: filterpane. So i cant just simply display the resultet from stored proc into grails

I am a newbie to grails. I have gone through grails froums, googled alot but unable to get any suggestions in this topic.

Grails provide transient fields to use when you need calculation fields in domain classes. You can wrap your calculation logic in a service, changing your domain class instances before using it.

class MyDomain {
  BigDecimal calcField

  static transients = ['calcField']
}

class MyService {
  def dataSource

  List<MyDomain> getInstances() {
    def instances = MyDomain.findAllBy...()
    instances.each { MyDomain ins ->
       doCalcField(ins)
    }

    instances

  }

  void doCalcField(MyDomain myDomain) {
    groovy.sql.Sql sql = new Sql(dataSource)
    //call procedure, returning values as OUT
    myDomain.calcField = calcField //assign out to domain instance
  }

}

Depending on your calculations, it's better to do it in the service directly, making your code database independent.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM