1

I have a situation where I have a hierarchy in my database like so:

 +-------+
 | Foo |
 +-------+
 | Id |
 | Type |
 | Color |
 +-------+
+------------+ +------------+
| A | | B |
+------------+ +------------+
| FooId | | FooId |
| Status | | Connection |
| SourceType | | Comments |
+------------+ +------------+

Now here are my java entities:

@Entity
@Table(name = "Foo", schema = "dbo")
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Foo {
 /* fields, getters, setters, constructors */
}
@Entity
@Table(name = "A", schema = "dbo")
@PrimaryKeyJoinColumn(name = "FooId")
public class A extends Foo {
 /* fields, getters, setters, constructors */
}
@Entity
@Table(name = "B", schema = "dbo")
@PrimaryKeyJoinColumn(name = "FooId")
public class B extends Foo {
 /* fields, getters, setters, constructors */
}

Then I have my repositories that I developed following this guide:

@NoRepositoryBean
public interface FooBaseRepository<T extends Foo> extends Repository<T, Integer> {
}
public interface FooRepository extends FooBaseRepository<Foo>, JpaRepository<Foo, Integer> {
}
public interface ARepository extends FooBaseRepository<A>, JpaRepository<A, Integer> {
}
public interface BRepository extends FooBaseRepository<B>, JpaRepository<B, Integer> {
}

I've gotten all this to work just fine no problem. But upon further inspection of this architecture I'm left wondering, why do I even need the FooBaseRepository. It says in the article and in the spring guide to define a base repo for "read only" operations but what's the point of going through the trouble of creating this base read-only repo when someone could just use the FooRepository to do read/write operations?

It seems this situation would only require the following repos:

public interface FooRepository extends JpaRepository<Foo, Integer> {
}
public interface ARepository extends JpaRepository<A, Integer> {
}
public interface BRepository extends JpaRepository<B, Integer> {
}

And everything would work just fine. What am I missing here?

asked Aug 22, 2018 at 3:46
1
  • Given that the linked article is a third-party blog (not from the Spring Data team, or an affiliate), any and all opinions expressed in that article are entirely opinion-based. That article also does not explain why they have suggested the given hierarchy. Given that the article has links under it to share feedback and comments, it will be worthwhile posting the question to the article author for their comments. Commented Aug 22, 2018 at 4:09

2 Answers 2

1

The only purpose of the FooBaseRepository is to have a home for methods that you want in all three repositories. If you don't have/want such methods you can drop it.

Spring Data uses the information about generics and method metadata of the actual repository interface. It does include in the analysis methods inherited from other interfaces, but it doesn't make a difference if a piece of information comes from the interface directly or from a super interface. So the inheritance structure of your repository interfaces doesn't matter as long as the resulting interface is the same (i.e. it has the same methods, same generic types).

Note: I don't think the article tries to say that the base interface is for read-only methods, it just says: you can mark it as such if it applies to your case and profit from whatever optimization the underlying JPA implementation does for read-only access.

answered Aug 22, 2018 at 5:25
Sign up to request clarification or add additional context in comments.

Comments

0

Inheritance to avoid duplication. for eg: if you added findById(Long id) without the FooBaseRepository, you will be duplicating the same findById(Long id) on all of your FooRepository, ARepository and BRepository interfaces. If you don't see a need for a common method then you're good with what you're doing but down the line you will see a need.

answered Aug 22, 2018 at 5:31

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.