Announcement Announcement Module
Collapse
No announcement yet.
Is Inheritance supported with Spring Data for JPA Repsitories? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Is Inheritance supported with Spring Data for JPA Repsitories?

    I have a simple inheritance hierarchy with one parent and one child.

    @Entity
    @DiscriminatorColumn(name = "itemType", discriminatorType = DiscriminatorType.STRING)
    public class A


    @Entity
    @Inheritance(strategy = InheritanceType.JOINED)
    @DiscriminatorValue("b")
    public class B extends A
    private Date published;

    I am using Spring Data to eliminate any boilerplate code and have defined a repository for B:

    public interface BRepository extends PagingAndSortingRepository<B, Long>
    findByPublishedIsNotNull();

    In this case, there is no actual implementation of the repository, Spring Data implements it during runtime.

    When I try to invoke the method, the SQL that is generated selects from table A only and the error message indicates that the column is missing (since it appears in table B). My expectation is that the tables would be joined so that the query can access the columns from both tables.

    Does Spring Data support inheritance? If so, what could I have missed? All the repositories and methods they defined worked fine until I added the inheritance.

    Thanks

  • #2
    Yes, Spring Data does indeed support inheritance beautifully. The issue that I ran into was caused by an annotation on the wrong class, the correct order should be:

    @Entity
    @Inheritance(strategy = InheritanceType.JOINED)
    @DiscriminatorColumn(name = "itemType", discriminatorType = DiscriminatorType.STRING)
    public class A


    @Entity
    @DiscriminatorValue("b")
    public class B extends A
    private Date published;

    Now the query is correctly generated by the interface:

    public interface BRepository extends PagingAndSortingRepository<B, Long>
    findByPublishedIsNotNull();

    Comment

    Working...
    X