如何使用动态查找器使用GORM排除某些列?

时间:2011-08-02 15:31:37

标签: grails gorm

我有following domain object

 class DbDeployment 
 { 
   static constraints = { 
    startDate(nullable: false) 
    endDate(nullable: true) 
    username(nullable: true) 
    fabric(nullable: false) 
    description(nullable: true) 
    status(nullable: true) 
    details(nullable: true) 
   } 

   static mapping = { 
    columns { 
     details type: 'text' 
     } 
   } 

  Date startDate = new Date() 
  Date endDate 
  String username 
  String fabric 
  String description 
  String status 
  String details // xml representation of the plan 
} 

我希望能够运行这样的查询:

DbDeployment.findAllByFabric("f1", params)

但我想确保不检索列details(可能很大)。

有办法做到这一点吗?

1 个答案:

答案 0 :(得分:6)

选项1:为大型数据字段创建关联

this answer的一个建议是将大数据属性分解为一个单独的域,这将导致它懒惰加载。例如:

class DbDeployment {
    Date startDate = new Date()
    Date endDate
    ...
    DbDeploymentDetails details
}

class DbDeploymentDetails {
    String details
    static belongsTo = DbDeployment
}

选项2:使用新域+映射到原始表

answer on a different question Grails mailing list question链接Burt Beckwith,{{3}}得到了很好的回答,但SO答案没有举例说明。为了后人的缘故,我会把他的例子从邮件列表中掏出来放在这里。

它涉及创建另一个没有大字段的域类,然后使用其static mapping闭包映射到另一个域的表。

class DbDeployment {
    Date startDate = new Date()
    Date endDate
    ...
    String details
}

class SimpleDbDeployment {
    Date startDate = new Date()
    Date endDate
    ...
    // do not include String details

    static mapping = {
        table 'db_deployment'
    }
}

然后你可以在SimpleDbDeployment上使用finder:

SimpleDbDeployment.findAllByFabric('f1', params)

Burt,如果您从邮件列表中重新回答您的示例,那就太棒了;我会把它从我的产品中删除并提升你的价值,因为你值得信任。