开发者

How to exclude some columns with GORM with dynamic finders?

I have the 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 
} 

I would like to be able to run a query like this:

DbDeployment.findAllByFabric("f1", params)

but I would like to ensure the column details (which is potentially huge) is not retrieved.

开发者_如何学Python

Is there a way to do this?


Option 1: Create an association for the large data field(s)

One suggestion from this answer is to break the large data property out into a separate domain, which will cause it to load lazily. Example:

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

class DbDeploymentDetails {
    String details
    static belongsTo = DbDeployment
}

Option 2: Using a new domain + mapping to the original table

An answer on a different question links to a Grails mailing list question which has a great answer from Burt Beckwith, but the SO answer doesn't give an example. For the sake of posterity I'll yoink his example from the mailing list and put it here.

It involves creating another domain class without the large field, and then using its static mapping closure to map to the other domain's table.

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'
    }
}

Then you can just use the finders on SimpleDbDeployment:

SimpleDbDeployment.findAllByFabric('f1', params)

Burt, it'd be great if you would re-answer with your example from the mailing list; I'll cut it out of mine and upvote yours, since you deserve the credit.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜