简体   繁体   English

Spring Data Repository JPA实体继承问题:无法实例化抽象类或接口

[英]Spring Data Repository JPA Entity Inheritance Issue: Cannot instantiate abstract class or interface

I'm using the below web app stack and when I run my TestNG test and the container gets initialized my test class fails to autowire the ExampleRepository with the below exception. 我正在使用下面的Web应用程序堆栈,当我运行TestNG测试并且初始化容器时,我的测试类无法自动连接ExampleRepository并带有以下异常。 Any ideas on how to map such a relationship? 关于如何映射这种关系有什么想法? When I start a jetty web server with the same web application I don't get any startup exceptions, so it could be an issue with AbstractTransactionalTestNGSpringContextTests. 当我使用相同的Web应用程序启动Jetty Web服务器时,没有任何启动异常,因此AbstractTransactionalTestNGSpringContextTests可能是一个问题。

Stack: 堆:

    Spring - 3.2.2.RELEASE
    Spring Data - 1.3.0.RELEASE
    Hibernate - 3.6.10.Final
    TestNG - 6.1.1

Exception: 例外:

Cannot instantiate abstract class or interface: com.test.Example; nested exception is org.hibernate.InstantiationException

JPA Entities: JPA实体:

@Entity
@Table(name = "EXAMPLE")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.STRING)
public abstract class Example {}


@Entity
@DiscriminatorValue("examplea")
public class ExampleA extends Example {}

@Entity
@DiscriminatorValue("exampleb")
public class ExampleB extends Example {}

Repository: 库:

public interface ExampleRepository extends JpaRepository<Example, Long> {}

TestNG Class: TestNG类:

public class ExampleTest extends AbstractTransactionalTestNGSpringContextTests{
      @Autowired
      private ExampleRepository exampleRepository;
}

Make Example class as Interface and let other classes implement it. 将Example类作为Interface并让其他类实现它。 As exception clearly says you cannot instantiate an abstract class. 异常明确表明您无法实例化抽象类。 Making Example class as interface you can instantiate its types ie the classes implementing it. 将Example类作为接口,您可以实例化其类型,即实现它的类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring jpa 存储库返回实体 class 而不是 dto 接口 - Spring jpa repository returns the entity class instead of the dto interface JPA — 无法实例化抽象 class 异常 - JPA — Cannot instantiate abstract class exception Spring Boot Data JPA 无法在 MockMVC 测试中自动装配存储库接口 - Spring Boot Data JPA cannot autowire repository interface in MockMVC test 删除独立实体spring jpa Repository接口 - Delete detached entity spring jpa Repository interface Spring Data JPA:从相同的存储库接口定义实例化多个存储库,每个存储库指向不同的数据库 - Spring Data JPA: Instantiate multiple repositories from same repository interface definitions, each pointing to a different database Spring Data JPA:创建抽象存储库 - Spring Data JPA : Creating an abstract repository Spring Data JPA存储库返回父类的实体 - Spring data JPA repository returns entity of parent class 在JPA和存储库接口中获取实体类 - Getting Entity Class in JPA and Repository Interface 我可以使用 Spring Data JPA 处理多个实体类的单个 JPA 存储库接口吗? - Can I have a single JPA repository interface handling multiple entity classes using Spring Data JPA? 为具有主键的实体定义弹簧数据JPA存储库接口,该主键也是JPA 2.x中的外键 - Defining a spring-data JPA repository interface for entity with primary key that is also a foreign key in JPA 2.x
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM