silent-box
silent-box

Reputation: 1666

Understanding Spring Data interfaces

I'm a little confused with some basic Spring Data concepts.

As I know, typical DAO level structure looks like this:

   - dao
     - MyFirstObjectDao
     - MySecondObjectDao
     - jpa
        - MyFirstObjectDaoImpl
     - jdbc
        - MySecondObjectDaoImpl

With this concept, I can use JPA as implementation for my first DAO interface, and JDBC - for the second one.

Now i want to understand Spring Repository abstraction. Every Spring Data tutorial specifies, that I should have an interface, which should extends from JpaRepository, for example. So having this interface, i've already locked with JPA, right?

What if i want to provide different implementations, like jpa/jdbc in DAO?

Upvotes: 0

Views: 130

Answers (1)

There's normally not a very good reason to want to mix JPA and JDBC persistence for the same objects; if you're already annotating everything as JPA entities, you might as well use the same persistence setup everywhere. You ought to rethink why you're wanting to do this.

That said, you could potentially do this if you really needed to:

interface FooBaseRepository extends PagingAndSortingRepository<Long, Foo> {}
interface FooJpaRepository extends FooBaseRepository, JpaRepository<Long, Foo> {}
interface FooJdbcRepository extends FooBaseRepository, JdbcRepository<Long, Foo> {}

@Autowired FooBaseRepository surpriseRepository

Update: If you're just talking about having different persistence strategies for different classes (that don't have relationships), there's nothing particularly complicated. You could mix Jpa and Jdbc repositories; they're just interfaces.

Upvotes: 1

Related Questions