Java Spring数据存储库JPA实体继承问题:无法实例化抽象类或接口
我正在使用下面的web应用程序堆栈,当我运行TestNG测试并初始化容器时,我的测试类无法自动连接ExampleRepository,出现以下异常。有没有关于如何描绘这种关系的想法?当我用同一个web应用程序启动jetty web服务器时,我没有收到任何启动异常,因此AbstractTransactionalTestNGSpringContextTests可能会出现问题 堆栈:Java Spring数据存储库JPA实体继承问题:无法实例化抽象类或接口,java,spring,hibernate,jpa,spring-data,Java,Spring,Hibernate,Jpa,Spring Data,我正在使用下面的web应用程序堆栈,当我运行TestNG测试并初始化容器时,我的测试类无法自动连接ExampleRepository,出现以下异常。有没有关于如何描绘这种关系的想法?当我用同一个web应用程序启动jetty web服务器时,我没有收到任何启动异常,因此AbstractTransactionalTestNGSpringContextTests可能会出现问题 堆栈: Spring - 3.2.2.RELEASE Spring Data - 1.3.0.RELEASE
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
@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> {}
public class ExampleTest extends AbstractTransactionalTestNGSpringContextTests{
@Autowired
private ExampleRepository exampleRepository;
}
例外情况:
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
@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> {}
public class ExampleTest extends AbstractTransactionalTestNGSpringContextTests{
@Autowired
private ExampleRepository exampleRepository;
}
JPA实体:
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
@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> {}
public class ExampleTest extends AbstractTransactionalTestNGSpringContextTests{
@Autowired
private ExampleRepository exampleRepository;
}
存储库:
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
@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> {}
public class ExampleTest extends AbstractTransactionalTestNGSpringContextTests{
@Autowired
private ExampleRepository exampleRepository;
}
将示例类作为接口,并让其他类实现它。 正如异常明确指出的,您不能实例化抽象类。
将Example类作为接口,您可以实例化它的类型,即实现它的类。Example是一个抽象类,因为它还有ExampleA和ExampleBThat类使用的实现代码,但您无法自动关联它的类型。您可以让您的类实现另一个接口,并扩展这个类,然后尝试自动连接接口类型。我想我理解您的意思。让示例成为一个具体的非抽象类,只创建一个ExampleA和ExampleB可以实现的接口。我将使用特定的repos
ExampleArespository
和ExampleBrespository