c12
c12

Reputation: 9827

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;
}

Upvotes: 2

Views: 3074

Answers (1)

Vineet Kasat
Vineet Kasat

Reputation: 1014

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 i.e the classes implementing it.

Upvotes: 1

Related Questions