Reputation: 83
I am trying to set up a Spring Data Jpa repository for an entity that inherits from another entity.
I am using Spring Boot 2.1.6 with Spring Data Jpa. I already researched the problem and looked at the documentation, but the cases I found, although similar, none showed very well how to handle repositories for classes when we have entity inheritance.
Here I have the main class:
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Person implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
// Getters and setters omitted
}
And here the class that inherits from Person:
@Entity
@PrimaryKeyJoinColumn(name="id")
public class Driver extends Person {
private String vehicleModel;
// Getters and setters omitted
}
So I created a filter to use in the repository:
public class DriverFilter {
private String vehicleModel;
// Getters and setters omitted
}
After I created the DriverQueries interface:
public interface DriverQueries {
public Page<Driver> filter(DriverFilter filter, Pageable pageable);
}
And then I made its implementation:
public class DriverImpl implements DriverQueries {
@Override
public Page<Driver> filter(DriverFilter filter, Pageable pageable) {
// code omitted
}
}
I create the base repository:
@NoRepositoryBean
public interface People<T extends Person> extends JpaRepository<T, Long> {
}
And finally, I create the repository for the inherited class:
@Repository
public interface Drivers extends People<Driver>, DriverQueries {
}
But when I run the program, I have the following exception:
Caused by: org.springframework.data.mapping.PropertyReferenceException: No property filter found for type Driver!
at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:94)
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:382)
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:358)
at org.springframework.data.mapping.PropertyPath.lambda$from$0(PropertyPath.java:311)
at java.util.concurrent.ConcurrentMap.computeIfAbsent(ConcurrentMap.java:324)
at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:293)
at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:276)
at org.springframework.data.repository.query.parser.Part.<init>(Part.java:81)
at org.springframework.data.repository.query.parser.PartTree$OrPart.lambda$new$0(PartTree.java:250)
at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:175)
at java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
at org.springframework.data.repository.query.parser.PartTree$OrPart.<init>(PartTree.java:251)
at org.springframework.data.repository.query.parser.PartTree$Predicate.lambda$new$0(PartTree.java:380)
at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:175)
at java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
at org.springframework.data.repository.query.parser.PartTree$Predicate.<init>(PartTree.java:381)
at org.springframework.data.repository.query.parser.PartTree.<init>(PartTree.java:93)
at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:78)
... 103 common frames omitted
If I create a repository for a normal class, following the same logic as the previous one, everything works perfectly.
Does anyone know what could be wrong?
Thank you!
Upvotes: 2
Views: 1200
Reputation: 146
This error disappears if we assigned to the class DriverImpl the correct name by analogy with it parent (DriverQueries) - DriverQueriesImpl. In this case, the inherited class shall be called as the parent plus the suffix Impl.
Spring is strict on this.
Upvotes: 2