How to define m: n relation with additional attributes in Squeryl?

Given an obsolete database with an m: n relation and some additional attributes for the relation, how can this be determined using squeryl. At the end of the table should look like this:

   + -------------- + + --------------- + + ---------------- +
   | TableA | | Rel_A_B | | TableB |
   + -------------- + ____ + --------------- + ____ + -------------- - +
   | id: Int | | tableA: int | | compkey_1: int |
   | (more attrs) | | tableB_1: int | | compkey_2: int |
   + -------------- + | tableB_2: int | | (more attrs) |
                         | value: Varchar | + ---------------- +
                         | date: Date |
                         + --------------- +

There is no problem defining three tables manually using squeryl. However, as far as I understand the documentation at the moment (0.9.4), there is no way to define a many-to-many relationship with additional attributes for the relationship.

This is why I defined three tables and two one-to-many relationships:


// TableA
class TableA(val id: Int, ...) extends KeyedEntity[Int] {
    def this() = this(0, ...)
}

// TableB
class TableB(val compkey1: Int, val compkey2: Int, ...) 
        extends KeyedEntity[CompositeKey2[Int, Int]] {

    def id = CompositeKey2(compkey1, compkey2)
}

// Rel_A_B
class RelAB(val tabA: Int, val tabB1: Int, val tabB2: Int, val value: String, 
            val date: Date) extends KeyedEntity[CompositeKey3[Int, Int, Int]] {

    def id = CompositeKey3(tabA, tabB1, tabB2)
}

Easily identify the relationship between TableA and RelAB. I use the usual one-to-many relationship:


val relA =
    oneToManyRelation(tableA, relAB).
    via((a, r) => a.id === r.tableA)

But I see no way to define a second relationship. I already tried to determine an additional composite value in the relationship table (called compkeyB) containing only the columns from table B, and compare it with the composite key of table B, but this does not work:


val relB =
    oneToManyRelation(tableB, relAB).
    via((b, r) => b.id === r.compkeyB)

It throws a type mismatch exception:

found   : org.squeryl.dsl.ast.LogicalBoolean
required: org.squeryl.dsl.ast.EqualityExpression

Any ideas how to solve this?

+5

All Articles