Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How To: Many-to-Many and DAO? #29

Closed
gcscaglia opened this issue May 27, 2016 · 3 comments
Closed

How To: Many-to-Many and DAO? #29

gcscaglia opened this issue May 27, 2016 · 3 comments

Comments

@gcscaglia
Copy link

First, I've searched everywhere but couldn't find a forum / detailed guide on Exposed framework so I'm resorting to post an issue here.

How do you model a many-to-many relationship using the DAO (Entities) method? Take these models:

package abcd

import org.jetbrains.exposed.dao.*
import org.jetbrains.exposed.sql.*

object Tags : IntIdTable(name = "tags") {
    val name = text("name")
}

object Posts : IntIdTable(name = "posts") {
    val uploadDate = datetime("upload_date")
}

object PostsTags : Table(name = "posts__tags") {
    val post = reference("post", Posts).primaryKey(0)
    val tag = reference("tag", Tags).primaryKey(1)
}

class Tag(id: EntityID<Int>) : IntEntity(id) {
    companion object : IntEntityClass<Tag>(Tags)

    var name by Tags.name
}

class Post(id: EntityID<Int>) : IntEntity(id) {
    companion object : IntEntityClass<Post>(Posts)

    var uploadDate by Posts.uploadDate
    val tags by Tag referrersOn PostsTags.tag // Obviously does not work. But how else?
}

How can I modify the Post entity so it has a tags property returning all tags associated to it through PostsTags? val tags by Tag referrersOn PostsTags.tag obviously doesn't work, and I couldn't find any similar alternative constructs.

In case such a scenario is not supported, how could one implement it? By looking on Referrers implementation I've got a general grasp on how to do it, but I'm struck at how to return a SizedIterable of tags from the results of a native SQL DSL query. Is there any mapping from SizedIterable<ResultRow> to SizedIterable<Entity>?

@Tapac
Copy link
Contributor

Tapac commented May 27, 2016

Maybe this will help:
val tags by Tag via PostsTags

@gcscaglia
Copy link
Author

Seems I was looking at it from the wrong direction. English is not my mother tongue, I had assumed via to meant something else altogether. Thank you very much.

I could send a pull request extending the current DAO samples to show via usage. Would it be desirable?

@Tapac
Copy link
Contributor

Tapac commented May 30, 2016

Ofc it will be great to improve documentations and samples. Will wait for your PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants