GORM: list of all domain instances owned by the user (root object)

I work with the grail after a while. I do not yet know how to implement it correctly.

I have a domain class (say, User) that contains a list that could potentially be any domain class (element, user, etc. etc.). Is there any way to do this out of the box?

Currently, I am doing this as follows:

I have a UserLink that contains the following properties:

class UserLink{ User user String className Long refId } 

Then I have a service that downloads all the 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 may lead to future performance issues.

What do you think? Do you have any better design ideas?

Thanks Nicolas

+4
source share
1 answer

Is this really any or only a specific subset of classes? I believe that you will have several more domain classes that are not directly related to the user.

If so, you can create a class or UserAsset interface with belongsTo=[user: User] prop and inherit / implement it.

Then find all the domain classes that implement it and query each with clazz.findByUser() , for example:

 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 say M: M, it changes only the last line, a request for userAssetClasses requested.

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

how

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

All Articles