简体   繁体   中英

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. 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.

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:

@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:

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

Make Example class as Interface and let other classes implement it. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM