Generic object load function for scala

Posted by Isaac Oates on Stack Overflow See other posts from Stack Overflow or by Isaac Oates
Published on 2010-03-12T15:04:58Z Indexed on 2010/03/12 15:07 UTC
Read the original article Hit count: 157

Filed under:
|

I'm starting on a Scala application which uses Hibernate (JPA) on the back end. In order to load an object, I use this line of code:

val addr = s.load(classOf[Address], addr_id).asInstanceOf[Address];

Needless to say, that's a little painful. I wrote a helper class which looks like this:

import org.hibernate.Session

class DataLoader(s: Session) {
  def loadAddress(id: Long): Address = {
    return s.load(classOf[Address], id).asInstanceOf[Address];
  }
  ...
}

So, now I can do this:

val dl = new DataLoader(s)
val addr = dl loadAddress(addr_id)

Here's the question: How do I write a generic parametrized method which can load any object using this same pattern? i.e

val addr = dl load[Address](addr_id)

(or something along those lines.)

I'm new to Scala so please forgive anything here that's especially hideous.

© Stack Overflow or respective owner

Related posts about scala

Related posts about parametrization