如何在Grails中建模存储过程记录? [英] How to model stored procedure records in Grails?

查看:117
本文介绍了如何在Grails中建模存储过程记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要调用一些存储过程来返回它们自己的记录,这些记录不是直接映射到表或视图。



我已经使用了存储过程过去有 groovy.sql.Sql 和普通(未建模)的地图,但是对于这个应用程序,我想用类似于域类的东西来模拟这些记录,以便定义数据类型,数据绑定,验证,与其他实体的关联等。



最好的方法是什么?

我应该将存储过程记录建模为适当的域类(实体),然后尝试禁用(或重新定义)其数据库持久性?如何?



我应该使用非域名POJO吗?我选择性地启用我需要的功能? (如使用 @Validatable 进行验证)那么我该如何处理关联? (当从SP返回的记录包含某个其他持久实体的外键时,会出现关联。)

解决方案

绑定,验证和关联以简单的方式进行维护,那么您应该使用域方法。



要禁用域类的数据库持久性,可以添加 static mapWith =none转换为域类,并且不会为该域类创建表。



示例实体class:

  @ToString 
public class SPTest {

Long idField

用户用户

GroupIntegrationKey integrationKey

静态约束= {
}

静态mapWith =none
}

存储过程语句:


SELECT id AS idField,user_id AS user,key AS integrationKey FROM
my_domain;

为了将SP的结果映射到实体,您可以使用结果转换器。

  Query query = session.createSQLQuery(CALL getSPData()); 

列表< Map> results = query.with {
resultTransformer = AliasToEntityMapResultTransformer.INSTANCE
list()
}

现在迭代列表并创建一个新的实体对象

  List< MyDomain> list = results.collect {
new MyDomain(it)
}

System.err.println(list)

缺点:$ b​​
$ b


  1. 您无法映射标识符字段

  2. 您必须再次遍历结果才能创建实体对象

  3. 您无法映射hasMany关系

如果您想要使用pojo,那么在这种情况下,您必须创建自己的getter版本才能获取关联的对象。


I need to call some stored procedures that return their own kinds of records, that are not directly mapped to tables or views.

I have used stored procedures in the past with groovy.sql.Sql and plain (unmodelled) Maps, but for this application I would like to model those records with something akin to domain classes, in order to define data types, data binding, validation, associations to other entities, and so on.

What is the best way to do so?

Should I model the stored procedure records as proper domain classes (entities) and then try to disable (or redefine) their database persistence? How?

Should I use non-domain POJOs where I selectively enable the features I need? (such as validation with @Validatable) How can I handle associations then? (Associations arise when the record returned from the SP contains a foreign key to some other persisted entity.)

解决方案

If you want data binding, validation and associations to be maintained in an easy way then you should go with the Domain approach.

To disable database persistence for a domain class you can add static mapWith = "none" to a domain class and a table won't be created for that domain class.

Sample Entity class:

@ToString
public class SPTest {

    Long idField

    User user

    GroupIntegrationKey integrationKey

    static constraints = {
    }

    static mapWith = "none"
}

Stored Procedure statement:

SELECT id AS idField, user_id AS user, key AS integrationKey FROM my_domain;

In order to map the result of the SP to the entity you can use result transformers.

Query query = session.createSQLQuery("CALL getSPData()");

List<Map> results = query.with {
    resultTransformer = AliasToEntityMapResultTransformer.INSTANCE
    list()
}

Now iterate over list and create a new entity object

List<MyDomain> list = results.collect {
    new MyDomain(it)
}

System.err.println(list)

Drawbacks:

  1. You can not map identifier field
  2. You would have to iterate over result again to create entity objects
  3. You can not map hasMany relationships

If you want to go with pojo, then in that case you would have to create your own version of getters to get associated objects.

这篇关于如何在Grails中建模存储过程记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆