简体   繁体   English

Grails域类与存储过程结果集的关联

[英]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...? 是否可以将存储过程的结果集关联到grails域类...?

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. 在grails视图(list.gsp)中,我使用的大多数插件都基于域类,例如:filterpane。 So i cant just simply display the resultet from stored proc into grails 所以我不能简单地从存储的过程中显示结果集到grails中

I am a newbie to grails. 我是个新手。 I have gone through grails froums, googled alot but unable to get any suggestions in this topic. 我经历了很多麻烦,在Google上搜索了很多,但是无法获得有关此主题的任何建议。

Grails provide transient fields to use when you need calculation fields in domain classes. 当需要域类中的计算字段时,Grails提供了临时字段供您使用。 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. 根据您的计算,最好直接在服务中进行操作,以使代码数据库独立。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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