Alex Rivera | Logout

Spring Data JPA : Creating an abstract repository

Asked 2012-03-28T12:39:07.793
14

Given the following classes:

@MappedSuperclass
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
@DiscriminatorColumn(name="animalType",discriminatorType=DiscriminatorType.STRING)
@QueryExclude
public abstract class Animal  {}

@Entity
@DiscriminatorValue("dog")
public class Dog {}

@Entity
@DiscriminatorValue("cat")
public class Cat {}

Is it possible somehow to configure a JPA Repository for Animal?

I've tried

public interface AnimalRepository extends JpaRepository<Animal,Long>

However this fails with:

java.lang.IllegalArgumentException: Not an managed type: Animal

Is there a way to configure this?

I'd like to be able to perform tasks like:

@Autowired
private AnimalRepository repository;

public void doSomething()
{
    Animal animal = repository.findById(123);
    animal.speak();
}
Edit
Report

1 Answer

10

I had similiar error. I solved it by adding mapping of my entity class to my persistence.xml file.

So maybe add something like this to your persistence.xml:

<persistence-unit>
...   
<class>yourpackage.Animal</class>
...
</persistence-unit>
answered 2012-07-25T07:12:09.710

Your Answer