GORM:列出属于User的所有域实例(根对象) [英] GORM: List all the domain instances belonging to User (root object)

查看:149
本文介绍了GORM:列出属于User的所有域实例(根对象)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一段时间以来我一直在与Grails合作。有一些我仍然不知道如何正确实现。



我有一个域类(比方说User),它包含一个List,它可能是任何域类(物品,用户等等)。有没有办法让这个开箱?



目前我按照以下方式操作:



我有一个UserLink,它包含以下属性:

  class UserLink {
用户用户
String className
Long refId
}

然后我有一个服务加载所有链接给一个给定的用户,然后链接相应的对象,并将它们作为列表返回。



我认为这种方法并不是最好的,并可能导致未来性能问题



您怎么看?你有更好的设计思路吗?



谢谢,
Nicolas

解决方案

它是真的吗,还是仅仅是某个类的子集?我相信你会有更多的领域类与用户没有直接关系。如果是这样,你可以创建一个 UserAsset 类或者接口,使用 belongsTo = [ user:User] prop,并继承/实现它。然后找到所有实现它的领域类,并用> clazz.findByUser()来查询每个领域类,如下所示: p>

  GrailsClass [] classes = grailsApplication.getArtefacts('Domain')
GrailsClass [] userAssetClasses =
classes。 clazz.findAll {UserAsset.class.isAssignableFrom(it)}
List< UserAsset> allUserAssets =
userAssetClasses.clazz * .findAllByUser(myUser).flatten()

编辑:如果我们在说M:M,它只会改变最后一行,查询 userAssetClasses 的方式。



UserAsset 将包含 hasMany = [users:User] 属性。



像:

  List< UserAsset> allUserAssets = userAssetClasses.clazz.collect {
类domainClass - >
it.withCriteria {
users {
eq('id',myUser.id)
}
}
} .flatten()


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). 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:

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.

Then find all domain classes implementing it, and query each with clazz.findByUser(), like:

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.

UserAsset will have a hasMany=[users:User] property.

Like:

List<UserAsset> allUserAssets = userAssetClasses.clazz.collect{ 
    Class domainClass ->
    it.withCriteria {
        users {
            eq('id', myUser.id)
        }
    }
}.flatten()

这篇关于GORM:列出属于User的所有域实例(根对象)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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