-
Jun 12th, 2012, 07:32 PM
#1
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
-
Jun 14th, 2012, 10:51 AM
#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();
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules