简体   繁体   English

GORM:列出属于用户(根对象)的所有域实例

[英]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.

相关问题 Grails 3.2中的域对象上是否存在默认的max to gorm list方法 - Is there a default max to gorm list method on domain object in grails 3.2 Grails / GORM / Hibernate:引用未保存的瞬态实例的域实例的属性列表 - Grails/GORM/Hibernate:List of the properties of a domain instance which reference unsaved transient instances 将域对象实例作为Map检索 - Retrieiving domain object instances as a Map 使用 GORM 保存方法时如何忽略域 object 上的字段 - How to ignore field on domain object when using GORM save method 日期字段的Grails GORM域对象命名约定和持久性后果 - Grails GORM domain object naming conventions for date fields, and persistence consequences Hibernate / GORM-处理多会话域对象更新 - Hibernate/GORM - Handling multi-session domain object updating 列表中一个域对象上的StaleObjectStateException会影响所有剩余的更新 - StaleObjectStateException on one domain object in a list affects all remaining updates Grails / GORM动态查找器通过关系ID而不是关系对象本身来获取域 - Grails/GORM dynamic finder to get the domain by its relation ID instead of relation object itself 查询已查询的域对象时,Grails GORM会给出休眠异常 - Grails GORM gives hibernate exception when querying domain object that was already queried 在Gorm条件中:按属性过滤域内的集合 - In Gorm Criteria: Filter collection inside domain by property
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM