Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Data JPA : Creating an abstract repository

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();
}
like image 956
Marty Pitt Avatar asked Mar 28 '12 12:03

Marty Pitt


1 Answers

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>
like image 110
kamuflage661 Avatar answered Oct 15 '22 22:10

kamuflage661