I want to use Spring Data JPA repositories in my project. Usually I create my own repository, let's say
interface ProductRepository extends JPARepository<Product, Long>
However, I want to serve a bit more complex case that fits the following:
I have a basic entity with common definition:
@MappedSuperclass public abstract class AbstractBaseEntity { @GeneratedValue(strategy = GenerationType.AUTO) @Id @Column(name = "ID", nullable = false) private Long id; ... }
I have all other entities extending the above one, for example:
@Entity @Table(name = "bread") public class Bread extends AbstractBaseEntity { @Column String type; ... }
and
@Entity @Table(name = "butter") public class Butter extends AbstractBaseEntity { @Column String weight; ... }
- I want that any repository that anyone creates, will only work with entities extending the AbstractBaseEntity. So I want to do something like this:
public interface MyBaseRepository<T> extends JpaRepository<T extends AbstractBaseEntity, Long>
Then define a couple of common methods and then use it as follows:
public interface BreadRepository <Bread, Long> extends MyBaseRepository
or
public interface ButterRepository extends MyBaseRepository
The problem is that I cannot do this. When I define MyBaseRepository, if I use:
MyBaseRepository<T extends AbstractBaseEntity> extends JpaRepository<T, Long>
I have an error that "entity does not have property type" when I running real query. If I use just
extends JpaRepository
I get an error that Object is not mapped. And if I try
JpaRepository<T extends AbstractBaseEntity , Long>
it just fails with unexpected binding error.
Do I miss anything or it is just not doable with Spring Data JPA?
Thanks!