java - Spring Data Repository JPA 实体继承问题 : Cannot instantiate abstract class or interface

标签 java spring hibernate jpa spring-data

我正在使用下面的Web应用程序堆栈,当我运行我的TestNG测试并且容器被初始化时,我的测试类无法 Autowiring ExampleRepository,并出现以下异常。关于如何映射这种关系有什么想法吗?当我使用相同的 Web 应用程序启动 jetty Web 服务器时,我没有收到任何启动异常,因此这可能是 AbstractTransactionalTestNGSpringContextTests 的问题。

堆栈:

    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 实体:

@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 {}

存储库:

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

TestNG 类:

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

最佳答案

将示例类作为接口(interface)并让其他类实现它。 异常清楚地表明您无法实例化抽象类。 将示例类作为接口(interface),您可以实例化其类型,即实现它的类。

关于java - Spring Data Repository JPA 实体继承问题 : Cannot instantiate abstract class or interface,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20487274/

相关文章:

java - 如何为原始类型创建 Mockito ArgumentCaptor?

java - sendRedirect 还是 request Dispatch 效率更高?

java - 隧道多部分文件

json - @ResponseBody 不使用 Hibernate Call 生成 json

java - hibernate 和斯卡拉

java - 如何在 Hibernate 和 Oracle 中使用命名查询将字符串作为数字进行搜索?

java - 如何解决嵌套异常是 java.lang.NoClassDefFoundError : org/springframework/security/web/util/AntPathRequestMatcher

java - 使用actionlistener调用同一个类中的void函数

java - 部署到 Jboss 7.1.0 EAP 时未指定持久性单元名称错误

java - 在 spring 中通过 resttemplate 发出的每个请求发送客户端证书的正确方法是什么?