Mar*_*itt 13 java spring spring-data 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 {}
Run Code Online (Sandbox Code Playgroud)
有可能以某种方式配置JPA存储库Animal吗?
我试过了
public interface AnimalRepository extends JpaRepository<Animal,Long>
Run Code Online (Sandbox Code Playgroud)
然而,这失败了:
java.lang.IllegalArgumentException:不是托管类型:Animal
有没有办法配置这个?
我希望能够执行以下任务:
@Autowired
private AnimalRepository repository;
public void doSomething()
{
Animal animal = repository.findById(123);
animal.speak();
}
Run Code Online (Sandbox Code Playgroud)
kam*_*661 10
我有类似的错误.我通过添加我的实体类到我的persistence.xml文件的映射来解决它.
所以可能会在你的persistence.xml中添加这样的东西:
<persistence-unit>
...
<class>yourpackage.Animal</class>
...
</persistence-unit>
Run Code Online (Sandbox Code Playgroud)
我遇到了这个问题,我找到了解决办法:你需要使用@MappedSuperclass OR @Inheritance,而不是两者都使用.Animal像这样注释你的类:
@Entity
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public abstract class Animal {}
Run Code Online (Sandbox Code Playgroud)
底层数据库方案将保持不变,现在您的通用AnimalRepository应该可以工作.持久性提供程序将执行内省并找出用于实际子类型的表.
我猜您正在运行 Hibernate 作为持久性提供程序,对吗?我偶然发现了 Hibernate 的这种情况的问题,因为针对 Hibernate 元模型的类型查找的行为与 JPA 中指定的内容不正确(有关详细信息,请参阅此错误)。所以看来你在这里有两个选择:
@Entity也更改为| 归档时间: |
|
| 查看次数: |
17032 次 |
| 最近记录: |