simou simou - 4 months ago 35
Scala Question

The Scala way of creating Spring Data JPA repositories

trait PersonRepository extends JpaRepository[Person, Long] { }

error: trait Repository is abstract; cannot be instantiated
[ERROR] @Repository

This little trait doesn't compile for obvious reasons. But how can I define a Spring Data JPA repository in Scala? (Scala 2.10.3)

A sample test case using scala-test:

@ContextConfiguration(locations = Array("classpath:/moduleContext.xml"), loader = classOf[AnnotationConfigContextLoader])
class PersonRepositoryTest extends FunSpec with Matchers {

@Autowired var personRepository: PersonRepository = _

describe("Person repository") {
it("should persist a person") {
val person = new Person()
val persistedPerson =
assert( >= 0)


Spring Data interfaces need to be interfaces :). Just make your repository one, and the infrastructure should be able to pick it up.

Make sure you set up the repositories correctly (either using <jpa:repositories base-package="…" /> or @EnableJpaRepositories).