Handling joins where data found in one table is optional

8 views
Skip to first unread message

ScalaNovice

unread,
Mar 28, 2012, 3:01:02 PM3/28/12
to orbroker
I have 2 tables: entries and photographs. An entry is allowed to
have no photos or N number of photos. They're foreign keyed the way
you would expect. Here's my classes and extractors:

case class Entry(
id: Option[Long],
name: String,
description: String,
date_added: java.util.Date,
total_files: Int,
categories: List[String],
files: Seq[Photo]
)
case class Photo(id: Long, name: String)

object EntryExtractor extends JoinExtractor[Entry] {
val key = Set("id")

def extract(row: Row, join: Join) = {
new Entry(
row.bigInt("id"),
row.string("name").get,
row.string("description").get,
row.timestamp("date_added").get,
row.integer("file_count").get,
row.array[String]("categories").get.toList,
join.extractSeq(PhotoExtractor)
)
}
}

object PhotoExtractor extends RowExtractor[Photo] {
val key = Set("file_id")

def extract(row: Row) = {
new Photo(
row.bigInt("file_id").get,
row.string("file_name").get
)
}
}

This seems to match up with the join examples given in the
documentation, but I keep getting a rune time error of
"java.util.NoSuchElementException: None.get". If I switch both of my
Photo class argument types to Option[T] the error goes away, but
entry.files contains a list that contains a single empty Photo
object. This doesn't seem right to me. What am I missing?

Nils Kilden-Pedersen

unread,
Mar 28, 2012, 3:28:48 PM3/28/12
to orbr...@googlegroups.com
Your PhotoExtractor should also be a JoinExtractor. Currently the key val is ignored, because the extractor is viewed as a RowExtractor (which has no concept of key).

Originally, the extractSeq only took JoinExtractor, but for 1-1 joins, a RowExtractor is faster, so it was allowed. But you must use a JoinExtractor for 1-N joins.

Let me know if that fixes the problem.

ScalaNovice

unread,
Mar 28, 2012, 3:58:03 PM3/28/12
to orbroker
Thank you for the quick reply and explaination. That certainly did
the trick.
Reply all
Reply to author
Forward
0 new messages