Search code examples
scalajdoscala-collections

Using JDOQL results in Scala


I'm trying to use a JDO with Google App Engine and Scala. The api for the execute returns Object (but it's really a java collection) and I want to get it into a scala list to iterate over it.

My code looks like this so far:

val pm = PMF.factory.getPersistenceManager
val query = "select from User " 
val gamelist:List[User] = List(pm.newQuery(query).execute.toArray:_ *)

The compile error at this point is toArray is not a member of Object. What is the best way to do the above? I tried to use .asInstanceOf[java.util.Collection[User]], but it was a failed attempt.


Solution

  • Use scala.collection.jcl.Conversions:

    import scala.collection.jcl.Conversions._
    ...
    // this gets you a List[User]
    val gameList = pm.newQuery(query).execute.asInstanceOf[java.util.List[User]].toList
    ...
    // or you can just iterate through the return value without converting it to List
    pm.newQuery(query).execute.asInstanceOf[java.util.List[User]] foreach (println(_))