java org.hibernate.InstantiationException:无法示例化抽象类或接口

im9ewurl  于 2023-05-05  发布在  Java
关注(0)|答案(7)|浏览(112)

我有以下实体:

@Entity
@Table(name = "davt_compensation_service")
@Inheritance(strategy = InheritanceType.JOINED)
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public abstract class Compensation<C extends Compensation> extends AutoIdBasedEntity {
    //properties, setters and getters, equals and hashcode overrides.
}

其余的继承自它(例如其中一个):

@Entity
@Table(name = "davt_compensation_service_relocation")
public class RelocationCompensation extends Compensation<RelocationCompensation> {
    //properties, setters and getters, equals and hashcode overrides
}

包含Compensations集合的父实体:

@Entity
@Table(name = "davt_compensation_case")
@EntityListeners({CompensationCaseNumberListener.class})
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class CompensationCase extends AutoIdBasedEntity {

    @Valid
    @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "compensationCase", orphanRemoval = true)
    @MapKey(name = "conceptType")
    private Map<ConceptType, Compensation> compensationServices = new HashMap<>();
}

最后,我有一个主实体CompensationFile,它有一个CompensationCase实体的集合:

@Entity
@Table(name = "davt_compensation_file")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class CompensationFile extends AutoIdBasedEntity {

    @Valid
    @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "compensationFile", orphanRemoval = true)
    private Set<CompensationCase> compensationCases;
}

这很奇怪,因为这曾经是工作,突然它开始工作了。由于某种原因,它只发生在更新和现有的CompensationCase和添加新的Compensation时。例如,如果我持久化一个全新的main CompensationCase,它可以顺利工作。我在主实体CompensationFile上使用JpaRepository方法保存来持久化整个内容,如下所示(以及它抛出异常的位置):
CompensationFileFormService:

@Service
public class CompensationFileFormService {
    @Autowired
    private CompensationFileRepository compensationFileRepository;

    @Autowired
    private CompensationFileFormConverter compensationFileFormConverter;

    @Autowired
    private CompensationCaseService compensationCaseService;        

    public CompensationFileForm saveCompensationFile(CompensationFileForm compensationFileForm) throws TaskNotFoundException, InvalidServiceTypeException, CompensationFileNotFoundException, CompensationCaseNotFoundException {

        CompensationFile compensationFile = compensationFileFormConverter.convertToCompensationFile(compensationFileForm);

        //EXCEPTION IS THROWN HERE
        CompensationFile newCompensationFile = compensationFileRepository.save(compensationFile);

        if (!newCompensationFile.getCompensationCases().isEmpty()) {
            for (CompensationCase compensationCase : newCompensationFile.getCompensationCases()) {
                compensationCaseTaskService.save(compensationCase, compensationCase.getAssignedPersonnel());
            }
        }

        return createCompensationFileForm(newCompensationFile.getId(), newCompensationFile.getControlId(), compensationFileForm.getSelectedCompensationCase().getId());
    }
}

CompensationFileRepository:

public interface CompensationFileRepository extends JpaRepository<CompensationFile, Long>, JpaSpecificationExecutor<CompensationFile> { 
}

抛出的异常:

26-Jul-2015 21:45:27.061 SEVERE [http-nio-9090-exec-11] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [dispatcher] in context with path [/nimchip] threw exception [Request processing failed; nested exception is org.springframework.webflow.execution.ActionExecutionException: Exception thrown executing [AnnotatedAction@1b7ee93d targetAction = [EvaluateAction@2902b1e5 expression = compensationFileFormService.saveCompensationFile(compensationFileForm), resultExpression = flowScope.compensationFileForm], attributes = map[[empty]]] in state 'save' of flow 'compensation/file/compensation-file' -- action execution attributes were 'map[[empty]]'] with root cause
 org.hibernate.InstantiationException: Cannot instantiate abstract class or interface: sijc.davt.compensation.model.concepts.Compensation
    at org.hibernate.tuple.PojoInstantiator.instantiate(PojoInstantiator.java:114)
    at org.hibernate.tuple.PojoInstantiator.instantiate(PojoInstantiator.java:136)
    at org.hibernate.tuple.entity.AbstractEntityTuplizer.instantiate(AbstractEntityTuplizer.java:727)
    at org.hibernate.persister.entity.AbstractEntityPersister.instantiate(AbstractEntityPersister.java:4440)
    at org.hibernate.type.EntityType.replace(EntityType.java:285)
    at org.hibernate.type.MapType.replaceElements(MapType.java:83)
    at org.hibernate.type.CollectionType.replace(CollectionType.java:570)
    at org.hibernate.type.TypeHelper.replace(TypeHelper.java:177)
    at org.hibernate.event.internal.DefaultMergeEventListener.copyValues(DefaultMergeEventListener.java:372)
    at org.hibernate.event.internal.DefaultMergeEventListener.entityIsPersistent(DefaultMergeEventListener.java:184)
    at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:157)
    at org.hibernate.internal.SessionImpl.fireMerge(SessionImpl.java:914)
    at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:896)
    at org.hibernate.engine.spi.CascadingAction$6.cascade(CascadingAction.java:288)
    at org.hibernate.engine.internal.Cascade.cascadeToOne(Cascade.java:380)
    at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:323)
    at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:208)
    at org.hibernate.engine.internal.Cascade.cascadeCollectionElements(Cascade.java:409)
    at org.hibernate.engine.internal.Cascade.cascadeCollection(Cascade.java:350)
    at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:326)
    at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:208)
    at org.hibernate.engine.internal.Cascade.cascade(Cascade.java:165)
    at org.hibernate.event.internal.DefaultMergeEventListener.cascadeOnMerge(DefaultMergeEventListener.java:439)
    at org.hibernate.event.internal.DefaultMergeEventListener.entityIsDetached(DefaultMergeEventListener.java:308)
    at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:151)
    at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:76)
    at org.hibernate.internal.SessionImpl.fireMerge(SessionImpl.java:904)
    at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:888)
    at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:892)
    at org.hibernate.ejb.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:879)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:344)
    at com.sun.proxy.$Proxy550.merge(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:291)
    at com.sun.proxy.$Proxy550.merge(Unknown Source)
    at org.springframework.data.jpa.repository.support.SimpleJpaRepository.save(SimpleJpaRepository.java:392)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:405)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:390)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:344)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:267)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodIntercceptor.invoke(CrudMethodMetadataPostProcessor.java:111)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
    at com.sun.proxy.$Proxy677.save(Unknown Source)
    at test.nimchip.davt.compensation.ui.service.CompensationFileFormService.saveCompensationFile(CompensationFileFormService.java:109)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.springframework.expression.spel.support.ReflectiveMethodExecutor.execute(ReflectiveMethodExecutor.java:112)
    at org.springframework.expression.spel.ast.MethodReference.getValueInternal(MethodReference.java:129)
    at org.springframework.expression.spel.ast.MethodReference.access$000(MethodReference.java:49)
    at org.springframework.expression.spel.ast.MethodReference$MethodValueRef.getValue(MethodReference.java:342)
    at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:88)
    at org.springframework.expression.spel.ast.SpelNodeImpl.getTypedValue(SpelNodeImpl.java:131)
    at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:299)
    at org.springframework.binding.expression.spel.SpringELExpression.getValue(SpringELExpression.java:84)
    at org.springframework.webflow.action.EvaluateAction.doExecute(EvaluateAction.java:75)
    at org.springframework.webflow.action.AbstractAction.execute(AbstractAction.java:188)
    at org.springframework.webflow.execution.AnnotatedAction.execute(AnnotatedAction.java:145)
    at org.springframework.webflow.execution.ActionExecutor.execute(ActionExecutor.java:51)
    at org.springframework.webflow.engine.ActionState.doEnter(ActionState.java:101)
    at org.springframework.webflow.engine.State.enter(State.java:194)
    at org.springframework.webflow.engine.Transition.execute(Transition.java:228)
    at org.springframework.webflow.engine.impl.FlowExecutionImpl.execute(FlowExecutionImpl.java:395)
    at org.springframework.webflow.engine.impl.RequestControlContextImpl.execute(RequestControlContextImpl.java:214)
    at org.springframework.webflow.engine.TransitionableState.handleEvent(TransitionableState.java:116)
    at org.springframework.webflow.engine.Flow.handleEvent(Flow.java:547)
    at org.springframework.webflow.engine.impl.FlowExecutionImpl.handleEvent(FlowExecutionImpl.java:390)
    at org.springframework.webflow.engine.impl.RequestControlContextImpl.handleEvent(RequestControlContextImpl.java:210)
    at org.springframework.webflow.engine.ViewState.handleEvent(ViewState.java:231)
    at org.springframework.webflow.engine.ViewState.resume(ViewState.java:195)
    at org.springframework.webflow.engine.Flow.resume(Flow.java:537)
    at org.springframework.webflow.engine.impl.FlowExecutionImpl.resume(FlowExecutionImpl.java:259)
    at org.springframework.webflow.executor.FlowExecutorImpl.resumeExecution(FlowExecutorImpl.java:169)
    at org.springframework.webflow.mvc.servlet.FlowHandlerAdapter.handle(FlowHandlerAdapter.java:228)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:868)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:644)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at test.nimchip.filter.LocaleConfigurerFilter.doFilterInternal(LocaleConfigurerFilter.java:35)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:154)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:150)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:199)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:110)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:50)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.session.ConcurrentSessionFilter.doFilter(ConcurrentSessionFilter.java:125)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)
    at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:344)
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:261)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:516)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1086)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:659)
    at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:223)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1558)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1515)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)

任何帮助都将不胜感激。

eit6fx6z

eit6fx6z1#

对于上述解决方案可能不起作用的其他人,给予考虑一下并尝试一下:当你让你的子实体继承父实体时,你将不能示例化父实体本身,但你应该示例化具有父属性的子实体-例如,我有一个抽象类Parent Profile,我有一个扩展它的子类StudentProfile。我尝试示例化一个配置文件列表,它抛出了异常(我在数据库的配置文件表中有数据,但我忘记将数据添加到StudentProfile表中(并通过for外键列连接它们-在我的情况下是id)我的解决方案是将相应的数据添加到StudentProfile表中,然后我就可以摆脱异常了。希望这有帮助!!

yqkkidmi

yqkkidmi2#

好了,原来从Compensation继承的一些实体具有ManyToOne关系的属性,并且它们缺少级联注解。因为它们不止一个属性,所以我没有收到TransientPropertyValueException,而是试图加载父Compensation实体,这是它可以关联实体的最接近的东西,而没有那些没有级联的字段。这意味着整个问题是由于缺少级联注解造成的。
例如:

@Entity
@Table(name = "davt_compensation_service_relocation")
public class RelocationCompensation extends Compensation<RelocationCompensation> {

    @Valid
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "phone_id")
    @ForeignKey(name = "FK_relocationCompensation_phone")
    private Phone phone = new Phone();

    @Valid
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "address_id")
    @ForeignKey(name = "FK_relocationCompensation_address")
    private Address address = new Address();

    //rest of properties, setters and getters, equals and hashcode overrides
}

变更为:

@Entity
@Table(name = "davt_compensation_service_relocation")
public class RelocationCompensation extends Compensation<RelocationCompensation> {

    @Valid
    @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinColumn(name = "phone_id")
    @ForeignKey(name = "FK_relocationCompensation_phone")
    private Phone phone = new Phone();

    @Valid
    @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinColumn(name = "address_id")
    @ForeignKey(name = "FK_relocationCompensation_address")
    private Address address = new Address();

    //rest of properties, setters and getters, equals and hashcode overrides
}
hivapdat

hivapdat3#

由于这是唯一的问题,出现了你谷歌搜索这个错误,我想分享另一个潜在的解决方案。当我的抽象类使用单表继承时,我遇到了一个问题,当我试图读取预加载的数据时,会出现“无法示例化抽象类或接口”异常。
我的persistence.xml包含了抽象类,但是它没有包含扩展抽象类的类。由于读取类的代码不是直接引用的,所以它们不会抛出无效的实体类异常。当它试图根据DiscriminatorColumn值示例化类时,它抛出了这个错误。将实现类添加到persistence.xml解决了此问题。

gkn4icbw

gkn4icbw4#

我的错误是,我忘了标记继承的实体(用@Entity),所以Hibernate没有具体的类来加载数据

rkue9o1l

rkue9o1l5#

在我的例子中,我使用dbRider来设置H2和测试数据,忘记添加一些带有数据的yml文件,这些数据依赖于未能初始化的类。

wgx48brx

wgx48brx6#

我遇到了同样的问题--我试图创建抽象类的示例,而不是具体子类

ccrfmcuu

ccrfmcuu7#

  • 在我的情况下(不是OP的情况):*

我用Bounded Type Parameters--<? extends YourAbstractClass>解决了它。
例如:

String queryStr = "select nn from NodeEvent nn";

//        TypedQuery<NodeEvent> query = em.createQuery(queryStr, NodeEvent.class);
//        List<NodeEvent> resultList = query.getResultList();
          // >> changed to // (NodeEvent is the abstract class where other classes inherit from)
          TypedQuery<? extends NodeEvent> query = em.createQuery(queryStr, NodeEvent.class);
          List<? extends NodeEvent> resultList = query.getResultList();

相关问题