[英]GORM: List all the domain instances belonging to User (root object)
I'm working with grails since a while. 从那以后我一直在工作。 There is something I still don't know how to implement correctly.
我仍然不知道如何正确实施某些功能。
I have a domain class (let's say User) which contains a List which can be potentially any domain class (Item, User, etc, etc). 我有一个域类(假设为User),其中包含一个List,该列表可能是任何域类(Item,User等)。 Is there a way to make this out of the box?
有没有一种方法可以做到这一点?
At the moment I'm doing it the following way: 目前,我正在按照以下方式进行操作:
I have a UserLink which contains following properties: 我有一个包含以下属性的UserLink:
class UserLink{
User user
String className
Long refId
}
Then I have a service which loads all links for a given user and then the corresponding objects in the link, and returns them as a list. 然后,我有一项服务,该服务为给定用户加载所有链接,然后加载链接中的相应对象,并将它们作为列表返回。
I think this approach is not the best, and could lead to future performance problems 我认为这种方法不是最好的方法,并且可能导致将来的性能问题
What do you think? 你怎么看? Do you have better design ideas?
您有更好的设计思路吗?
Thanks, Nicolas 谢谢,尼古拉斯
Is it really any, or only a certain subset of classes? 是真的吗,还是只是某个类的子集? I believe you'll have some more domain classes not directly related to User.
我相信您还会有更多与用户不直接相关的域类。
If so, you can create a UserAsset
class or interface with a belongsTo=[user: User]
prop, and inherit/implement it. 如果是这样,您可以创建一个
UserAsset
类或接口,并使用belongsTo=[user: User]
道具,并继承/实现它。
Then find all domain classes implementing it, and query each with clazz.findByUser()
, like: 然后找到实现它的所有域类,并使用
clazz.findByUser()
查询,例如:
GrailsClass[] classes = grailsApplication.getArtefacts('Domain')
GrailsClass[] userAssetClasses =
classes.clazz.findAll { UserAsset.class.isAssignableFrom(it) }
List<UserAsset> allUserAssets =
userAssetClasses.clazz*.findAllByUser(myUser).flatten()
edit : If we're talking M:M, it only changes last line, the way userAssetClasses
are queried. 编辑 :如果我们在说M:M,它只会更改最后一行,即查询
userAssetClasses
的方式。
UserAsset
will have a hasMany=[users:User]
property. UserAsset
将具有hasMany=[users:User]
属性。
Like: 喜欢:
List<UserAsset> allUserAssets = userAssetClasses.clazz.collect{
Class domainClass ->
it.withCriteria {
users {
eq('id', myUser.id)
}
}
}.flatten()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.