java org.hibernate.InstantiationException:无法实例化抽象类或接口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31644027/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
org.hibernate.InstantiationException: Cannot instantiate abstract class or interface
提问by Nimchip
I have the following entity:
我有以下实体:
@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.
}
The rest inherit from it (one of them for example):
其余的继承自它(例如其中之一):
@Entity
@Table(name = "davt_compensation_service_relocation")
public class RelocationCompensation extends Compensation<RelocationCompensation> {
//properties, setters and getters, equals and hashcode overrides
}
A parent entity that contains a collection of Compensations
:
包含以下集合的父实体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<>();
}
Last but not least, I have a the main entity CompensationFile
which has a collection of CompensationCase
entities:
最后但并非最不重要的是,我有一个包含实体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;
}
It's strange because this used to work and suddenly it started working up. For some reason it only happens when updating and existing CompensationCase
and adding a new Compensation
. For example, if I am persisting an entirely new main CompensationCase
, it works without a hitch. I use the JpaRepository
method save on the main entity CompensationFile
to persist the entire thing as shown below (and where it is throwing the exception):
这很奇怪,因为这曾经有效,但突然开始起作用了。出于某种原因,它仅在更新和现有CompensationCase
并添加新的Compensation
. 例如,如果我坚持一个全新的 main CompensationCase
,它可以顺利运行。我使用JpaRepository
主实体上的 save 方法CompensationFile
来持久化整个事物,如下所示(以及它在哪里抛出异常):
CompensationFileFormService:
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> {
}
Exception thrown:
抛出异常:
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.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.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@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
}
0(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)
Any help at all will be appreciated.
任何帮助都将不胜感激。
采纳答案by Nimchip
Ok, so it turns out some of the entities that inherited from Compensation
had properties with ManyToOne
relations and they were missing cascade annotations. Because they were more than one property, I received no TransientPropertyValueException
and instead it tried to load the parent Compensation
entity which was the closest thing that it could associate the entity to without those fields that weren't being cascaded. Which means that this whole problem was due to missing cascade annotations.
好的,所以事实证明,继承自的一些实体Compensation
具有具有ManyToOne
关系的属性,并且它们缺少级联注释。因为它们不仅仅是一个属性,所以我没有收到TransientPropertyValueException
,而是尝试加载父Compensation
实体,这是它可以将实体关联到的最接近的东西,而没有那些没有级联的字段。这意味着整个问题都是由于缺少级联注释。
For example:
例如:
@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
}
Changed to:
变成:
##代码##回答by Alex Vulchev
For others that the above solutions might not work, give this a thought and a try afterwards: When you have you child entities inherit parent entity you will not be able to instantiate the Parent itself but you should instantiate the child that has the parents properties - For example i had Parent abstract class Profile and i had a child class StudentProfile that extended it. I tried instantiating a list of profiles and it threw the exception(I had data in the profile table in the database but i forgot to add data to the StudentProfile table(and connect them through the for foreign key column - in my case id) My solution was to add the corresponding data to the StudentProfile table and then i was able to get rid of the exception. Hope this helps!!
对于上述解决方案可能不起作用的其他人,请考虑一下并在之后尝试:当您让子实体继承父实体时,您将无法实例化父实体,但您应该实例化具有父属性的子实体 -例如,我有父抽象类 Profile,我有一个扩展它的子类 StudentProfile。我尝试实例化配置文件列表并抛出异常(我在数据库的配置文件表中有数据,但我忘记将数据添加到 StudentProfile 表(并通过 for 外键列连接它们 - 在我的情况下为 id)我的解决方案是将相应的数据添加到 StudentProfile 表中,然后我就能够摆脱异常。希望这有帮助!!
回答by Mike
Since this is the only question that comes up you google search this error, I wanted to share another potential solution. I had an issue when my abstract class used Single Table inheritance and gave the 'Cannot instantiate abstract class or interface' exception when I tried to read my pre-loaded data.
由于这是您在谷歌搜索此错误时出现的唯一问题,因此我想分享另一个潜在的解决方案。当我的抽象类使用单表继承时遇到问题,并在我尝试读取预加载数据时给出“无法实例化抽象类或接口”异常。
My persistence.xml contained the abstract class, but it did not include the classes that extend the abstract class. Since the code to read the classes are not directly referenced they do not throw a invalid entity class exception. When it tried to instantiate the class based on the DiscriminatorColumn value it threw this error. Adding the implementation class to persistence.xml fixed this issue.
我的persistence.xml 包含抽象类,但它不包含扩展抽象类的类。由于读取类的代码没有被直接引用,因此它们不会抛出无效的实体类异常。当它尝试根据 DiscriminatorColumn 值实例化类时,它抛出了这个错误。将实现类添加到persistence.xml 解决了这个问题。
回答by cljk
My fault was, that I forgot to mark the inherited Entity as such (with @Entity) and so Hibernate had no concrete class to load the data
我的错是,我忘记将继承的实体标记为这样(使用@Entity),因此 Hibernate 没有具体的类来加载数据