1

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:

  1. 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;
        ...
    }
    
  2. 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;
...
}
  1. 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!

user3155208
  • 191
  • 1
  • 9
  • See [related](http://stackoverflow.com/questions/25237664/use-abstract-super-class-as-parameter-to-spring-data-repository/25241995#25241995) – Paul Samsotha Sep 17 '14 at 07:35
  • @Inheritence does not make sence in my case, looks like it is not possible. Thank you very much for answering so fast! – user3155208 Sep 17 '14 at 07:50

1 Answers1

0

I know this is an old thread but I had the same kind of error trying to define a repository like your MyBaseRepository<T extends AbstractBaseEntity> extends JpaRepository<T, Long>.

In my case the error was :

Failed to create query for method public abstract MyBaseRepository.someMethod(); Not an entity: AbstractBaseEntity

The point was that JPA was trying to implements all interfaces that extends JpaRepository and can't do it on MyBaseRepository<T> since <T> isn't a concrete class and AbstractBaseEntity not an @Entity.

The solution was to annotate MyBaseRepository<T> with @NoRepositoryBean to prevent JPA trying to implements this repository. And so, only your BreadRepository will be implemented.

MatthieuBlm
  • 43
  • 1
  • 8