java Spring Data JPA:创建抽象存储库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9907666/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 22:50:24  来源:igfitidea点击:

Spring Data JPA : Creating an abstract repository

javaspringspring-dataspring-data-jpa

提问by Marty Pitt

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?

是否有可能以某种方式配置 JPA 存储库Animal

I've tried

我试过了

public interface AnimalRepository extends JpaRepository<Animal,Long>

However this fails with:

但是,这失败了:

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

java.lang.IllegalArgumentException:不是托管类型:动物

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();
}

回答by kamuflage661

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.xml中添加这样的东西:

<persistence-unit>
...   
<class>yourpackage.Animal</class>
...
</persistence-unit>

回答by CFL_Jeff

I was having this exact problem, and I found the solution: You need to either use @MappedSuperclassOR@Inheritance, not both together. Annotate your Animalclass like this:

我遇到了这个确切的问题,我找到了解决方案:您需要使用@MappedSuperclassOR@Inheritance,而不是同时使用。Animal像这样注释你的类:

@Entity
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public abstract class Animal  {}

The underlying database scheme will remain the same, and now your generic AnimalRepositoryshould work. The persistence provider will do the introspection and find out which table to use for an actual subtype.

底层数据库方案将保持不变,现在您的泛型AnimalRepository应该可以工作了。持久性提供者将进行自省并找出用于实际子类型的表。

回答by Oliver Drotbohm

I guess you're running Hibernate as your persistence provider, right? I've stumbled over problems with this scenario with Hibernate as the type lookup against the Hibernate metamodel doesn't behave correctly contradicting what's specified in the JPA (see this bug for details). So it seems you have two options here:

我猜你正在运行 Hibernate 作为你的持久性提供者,对吧?我偶然发现了 Hibernate 的这种情况的问题,因为针对 Hibernate 元模型的类型查找与 JPA 中指定的行为不正确(有关详细信息,请参阅此错误)。所以看起来你在这里有两个选择:

  1. Change the abstract superclass to be an @Entityas well
  2. Switch to a different persistent provider
  1. 将抽象超类也更改@Entity
  2. 切换到不同的持久化提供者

回答by Peter Isberg

You need to include Animal-class in your @EntityScan in your webconfig

您需要在 webconfig 的 @EntityScan 中包含 Animal-class