-
Notifications
You must be signed in to change notification settings - Fork 35
Fetching individual members from entities (or more generically, easier way to use SORM's SQL abstractions) #24< 8000 /span>
Comments
Concerning your model I'd strongly suggest simply referring to multiple tags from post with either a case class Post(title: String, content: String, tags: Set[Tag])
case class Tag(name: String) Concerning your suggestion, something in the spirit of it really is a "must have" feature and it is planned to be implemented in 0.4 or later. If you implement it in 0.3, I'll gladly merge it. But I must warn you that it'll be really hard and will require you to dive very deeply into the current architecture of SORM. |
I tried to use a generic example but ended up choosing one that didn't actually represent my problem. case class Comment(replies: Set[Comment]) Does not work because recursive entities are not supported (it causes an infinite loop during runtime by the way). So I reverted to: case class Comment
case class CommentChild(parent: Comment, child: Comment) Because I wanted to be able to partially fetch for pagination, which I assume would not be possible with case class CommentChildren(parent: Comment, children: Set[Comment]) What should I do then other than writing the query manually? Is there a way for me to use SORM's SQL abstractions to do it, or would it be too complicated? Sorry for the long posts, maybe I should move this to a StackOverflow question. |
Yes, recursive entities are currently unsupported, but you've presented an example good enough for such support to be implemented. Since SORM works only with immutable values there's no way to pass a self-referring value to it, so it even looks like a safe thing. I'm seriously considering that. For now you can go with this approach: case class Comment
case class CommentChild(parent: Comment, child: Comment) You can query for chunks like that: val secondTenComments =
db.query[CommentChild]
.whereEqual("parent", parentComment).offset(10).limit(10)
.fetch()
.map(_.child) But, of course, querying for deeper replies will require more queries.
No problem. Since such a question induces a discussion, here is a better place. StackOverflow is for specific "How to" questions, which don't induce prolonged discussion. I've also just created a sorm-framework@googlegroups.com mailing list for general discussions. |
That is my current approach as well, but I have not yet found a way around it fetching the parent comment repeatedly with every |
I'm afraid there is none yet, except for the manual SQL. That is of course until the partial fetching is realized. |
After reading some of the questions and answers on StackOverflow while modelling a many-to-many relationship I decided to go with the relationship-as-a-class approach:
Because I wanted to be able to fetch only part of the result set. With this I can do something like:
But if I am not mistaken this will fetch the Tag again and construct an instance of it from the DB for every single row in the relationship. This seems really inefficient, so I'll probably have to resort to
fetchWithSql
, which is okay, but after writing the query I realized I'm hardcoding many of SORM's conventions, and that SORM already has a mechanism to map attributes to query paths (for the where clauses in queries).So I ask if it would be too hard to use the existing filter infrastructure to fetch individual columns from the database.
Something like:
I would be willing to try and implement this myself with some guidance of where I need to look since this seems more complex than the other fixes.
.
The text was updated successfully, but these errors were encountered: