Hi everyone, what are people using to do nested-se...
# questions
d
Hi everyone, what are people using to do nested-sets/tree structured data? I am upgrading our app to grails 5 and seems like an old dev created his own custom plugin and I just want to reevaluate if thats the best option
j
What is it that you want the application to be able to do with or to the nested-sets/tree structured data?
d
In our case its location data - so Facility is the top level each facility can a number of departments each dpt can have sub locations. Staff is assigned to these locations, items are stored at the locations. So the main purpose of the locations is for searching of those entities assigned to each location
j
Do you want to persist that info to a relational database?
d
sorry yea we have that stored in MySQL
j
GORM provides facilities for managing relationships. https://gorm.grails.org/8.1.0/hibernate/manual/index.html#gormAssociation is a good starting point. If you have specific questions, here is a good place to ask those.
"...an old dev created his own custom plugin and I just want to reevaluate if thats the best option" - Knowing what that plugin is doing would help identify if it is the best option.
j
We use a common Location Table, that then other locations extend ... i.e. Area - marketing term for multiple countries Country Port Facility We also denormalize the relationship to the object via beforeValidate() & beforeInsert() methods so that we set these fields prior to insertion/update. This then allows us to do things like: select id from location where area='myareacode' Which would retrieve all locations under that area. To make it more performant, we only update the fields if the field null or isDirty() . We used to use belongsTo and separate tables but having this data denormalized lets you do one query for all locations if you need and you can slice that query by type if needed too - i.e. query on Port where area='myareacode' if you want ports, etc.
The gotcha for us was the Location class has to be abstract & annotated with
@DirtyCheck
so everything is one table.
d
thanks for the information!
👍 1
u
I found this a good reference for thinking about a similar problem a couple years ago in an old project. https://speakerdeck.com/sdelamo/mapping-a-tree-with-gorm-and-grails?slide=4
❤️ 1
👍 1