![](/img/trans.png)
[英]Spring jpa repository returns the entity class instead of the dto interface
[英]Spring Data Repository JPA Entity Inheritance Issue: Cannot instantiate abstract class or interface
我正在使用下面的Web应用程序堆栈,当我运行TestNG测试并且初始化容器时,我的测试类无法自动连接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
例外:
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;
}
将Example类作为Interface并让其他类实现它。 异常明确表明您无法实例化抽象类。 将Example类作为接口,您可以实例化其类型,即实现它的类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.