Question

@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)
    }
  }
}
Was it helpful?

Solution

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

OTHER TIPS

Just remove @Repository

This is working for me:

trait CityRepository extends JpaRepository[City, String] {
   def findById(id:String):java.util.List[City]
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top