simou simou - 2 months ago 19
Scala Question

The Scala way of creating Spring Data JPA repositories

@Repository
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:

@RunWith(classOf[JUnitRunner])
@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 = personRepository.save(person)
assert(persistedPerson.id >= 0)
}
}
}

Answer

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).