开发者

Grails many-to-one expose id without lazy load?

开发者 https://www.devze.com 2023-02-21 05:46 出处:网络
Grails - H开发者_运维百科ow can I expose the id of a many-to-one association property without forcing a load of the entire object?

Grails - H开发者_运维百科ow can I expose the id of a many-to-one association property without forcing a load of the entire object?

class Task {
  User belongsTo
  User createdBy
}
class User {
  ...
}

Can I get get the User ids from Task without causing lazy loading of the entire User objects? Is there a way to do this in general, or will it involve a custom query for each class? This actually involves DTO objects to return via BlazeDS - all I need are the ids, not the associated objects necessarily.


Grails by default has lazy association enabled so you don't have to worry about the loading of all chained objects unless you enable eager fetching. further more you could use named queries or hibernate criteria projections: e.g.

 def taskCriteria = Task.createCriteria()  
 def users = taskCriteria.list{//or get
     projections {
         property "id"
     } 
     user{
         eq("userName", "Joe")
     }   
 }

the above will pull only the id of a task for a user called Joe.


Apparently you need to use the magic notation belongsToId and createdById - see GRAILS-2570

0

精彩评论

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