Spring Data JPA:创建一个抽象存储库 [英] Spring Data JPA : Creating an abstract repository
本文介绍了Spring Data JPA:创建一个抽象存储库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
给出以下类:
@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 {}
是否有可能以某种方式为 Animal
配置JPA存储库?
Is it possible somehow to configure a JPA Repository for Animal
?
我已经尝试了
public interface AnimalRepository extends JpaRepository<Animal,Long>
然而这失败了:
java.lang.IllegalArgumentException:不是托管类型:Animal
java.lang.IllegalArgumentException: Not an managed type: Animal
有没有办法配置?
我希望能够执行以下任务:
I'd like to be able to perform tasks like:
@Autowired
private AnimalRepository repository;
public void doSomething()
{
Animal animal = repository.findById(123);
animal.speak();
}
推荐答案
我有类似的错误。我通过添加我的实体类到我的persistence.xml文件的映射来解决它。
I had similiar error. I solved it by adding mapping of my entity class to my persistence.xml file.
所以可能会在你的persistence.xml中添加这样的东西:
So maybe add something like this to your persistence.xml:
<persistence-unit>
...
<class>yourpackage.Animal</class>
...
</persistence-unit>
这篇关于Spring Data JPA:创建一个抽象存储库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文