Reputation: 5862
I have the following grails domain class, containing a self-relationship.
class Message {
static hasMany = [replies: Message]
Message isReplyTo
User author
String title
String text
Date createdAt
Date lastUpdated
}
I want to write a query that can do two things
If a message is a parent, i.e it is not a reply to any other message, retrieve a list of messages that build a conversation starting with that message.
Message 1
---- Reply 1
---- Reply 2
-------- SubReply 1
Given a message that is a reply, to another message, also build a list same as above, including that parent that the given message (reply) falls under.
I have thought about this but can't quite think of a possible way of doing this, since there is no Conversations
domain class that ties message belonging to the same conversation together. So I am hoping that there is some sort recursive query that would help me achieve this.
Upvotes: 2
Views: 284
Reputation: 12416
There are couple ways to handle this type of structure and there's no shortage of examples on the web about this type of tree based relationship.
In the simplest approach; you could have a Parent
Child
structure where a null
parent would signal the top of the tree and a 'null' child would signal the end of that branch. This is a simple and effective structure but, it can be difficult to capture metrics because you'll always have to recursively search the entire thread.
Message{
...
Message parent
Message child
}
This post shows a good recursion in GSP example:
Another pattern for this is materialized path. It's a little trickery to implement but, easy to work once it's in place. Materialized paths are also much easier to collect metrics for like count of reply's and doing breadcrumbs are much easier too.
Message{
...
Message parent
String path //would like something like this: 1/2/3/4
}
For an example search of Materialized path check out google or this post.
Upvotes: 3