我正在使用下面的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/