mapperdao manytoone - getting hold of id's

32 views
Skip to first unread message

Jan Hansen

unread,
Oct 26, 2012, 7:31:49 AM10/26/12
to scala...@googlegroups.com
I try to get hold of the id of an entity through a
manytoone mapping, but unsuccessful so far.

This is a very stripped down version,
and I hope my question still make sense.

=== ENTITIES =====
object A_Entity extends Entity[IntId, A] {
val id = key("id") sequence seq("A_SEQ") autogenerated (_.id)
val name = column("name") to (_.name)

def constructor(implicit m:ValuesMap) =
new A(name) with IntId with Persisted {

val id: Int = A_Entity.id
}
}

-----

object B_Entity extends Entity[IntId, B] {
val id = key("id") sequence seq("B_SEQ") autogenerated (_.id)
val A_item = manytoone(A_Entity) foreignkey("A_item_id") to (_.A_item)

def constructor(implicit m:ValuesMap) =
new B(A_item) with Persisted with IntId {

val id: Int = B_Entity.id
}
}

=== MODELS =====
case class A (
val name: String,
)

object A {
def empty = new A("")
}

-----

case class B(
val title: String,
val A_item:A,
)

object B {
def empty = new B(new A("")
}

=== DAOS ===
abstract class A_Dao extends TransactionalIntIdCRUD[A] with IntIdAll[A] {
entity = A_Entity
}

-----

abstract class B_Dao extends TransactionalIntIdCRUD[B] with IntIdAll[B] {
val entity = B_Entity
}


=== BUSINESS LOGIC ===
val B_all = B_Dao.all

b <- B_all
if (true) b.A_item.id // DOES NOT EXIST

I can access the A_item.name but not the A_item.id
How can I get hold of A_item's id ?




Flávio W. Brasil

unread,
Oct 26, 2012, 9:52:14 PM10/26/12
to Jan Hansen, scala...@googlegroups.com
Do you know Activate? Is much simpler:

case class A(name: String) extends Entity
case class B(title: String, item: A) extends Entity

val bAll = all[B]
val ids = bAll.map(_.id)

The ID is available at any time and there is no need for empty constructors, DAOs and separate entity declaration.

-- 
Flávio W. Brasil

Kostas kougios

unread,
Oct 31, 2012, 7:49:35 PM10/31/12
to scala...@googlegroups.com, sl07...@hotmail.com
Try Helpers.intIdOf(...). There are more useful methods available in that object.

Reply all
Reply to author
Forward
0 new messages