Unexpected error No default constructor for entity

Hibernate uses the default constructor to create entity objects. If the default constructor is not available in any of the entities, InstantiationException will be thrown from hibernate.



Exception

Whitelabel Error Page
 This application has no explicit mapping for /error, so you are seeing this as a fallback.
 Fri Oct 04 17:01:43 IST 2019
 There was an unexpected error (type=Internal Server Error, status=500).
 No default constructor for entity: : com.yawintutor.Book; nested exception is org.hibernate.InstantiationException: No default constructor for entity: : com.yawintutor.Book
 org.springframework.orm.jpa.JpaSystemException: No default constructor for entity:  : com.yawintutor.Book; nested exception is org.hibernate.InstantiationException: No default constructor for entity:  : com.yawintutor.Book
     at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:352)
     at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:254)
     at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:528)
     at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:61)
     at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:242)
     at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:153)
     at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
     at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:144)
     at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
     at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$ExposeRepositoryInvocationInterceptor.invoke(CrudMethodMetadataPostProcessor.java:364)
     at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
     at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:93)
     at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
     at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:61)
     at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
     at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
     at com.sun.proxy.$Proxy90.findAll(Unknown Source)
     at com.yawintutor.BookController.findAll(BookController.java:28)
     at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
     at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
     at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
     at java.base/java.lang.reflect.Method.invoke(Unknown Source)
     at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190)
     at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
     at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105)
     at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:893)
     at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:798)
     at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
     at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040)
     at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)
     at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
     at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898)
     at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
     at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
     at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
     at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
     at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)
     at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
     at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)
     at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
     at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:94)
     at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
     at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
     at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
     at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
     at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
     at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:526)
     at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
     at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
     at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
     at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
     at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
     at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
     at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:860)
     at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1589)
     at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
     at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
     at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
     at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
     at java.base/java.lang.Thread.run(Unknown Source)
 Caused by: org.hibernate.InstantiationException: No default constructor for entity:  : com.yawintutor.Book
     at org.hibernate.tuple.PojoInstantiator.instantiate(PojoInstantiator.java:85)
     at org.hibernate.tuple.PojoInstantiator.instantiate(PojoInstantiator.java:105)
     at org.hibernate.tuple.entity.AbstractEntityTuplizer.instantiate(AbstractEntityTuplizer.java:685)
     at org.hibernate.persister.entity.AbstractEntityPersister.instantiate(AbstractEntityPersister.java:5027)
     at org.hibernate.internal.SessionImpl.instantiate(SessionImpl.java:1711)
     at org.hibernate.internal.SessionImpl.instantiate(SessionImpl.java:1695)
     at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1736)
     at org.hibernate.loader.Loader.getRow(Loader.java:1598)
     at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:742)
     at org.hibernate.loader.Loader.processResultSet(Loader.java:1002)
     at org.hibernate.loader.Loader.doQuery(Loader.java:960)
     at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:351)
     at org.hibernate.loader.Loader.doList(Loader.java:2787)
     at org.hibernate.loader.Loader.doList(Loader.java:2770)
     at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2604)
     at org.hibernate.loader.Loader.list(Loader.java:2599)
     at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:505)
     at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:395)
     at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:220)
     at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1537)
     at org.hibernate.query.internal.AbstractProducedQuery.doList(AbstractProducedQuery.java:1538)
     at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1506)
     at org.hibernate.query.Query.getResultList(Query.java:132)
     at org.hibernate.query.criteria.internal.compile.CriteriaQueryTypeQueryAdapter.getResultList(CriteriaQueryTypeQueryAdapter.java:74)
     at org.springframework.data.jpa.repository.support.SimpleJpaRepository.findAll(SimpleJpaRepository.java:346)
     at org.springframework.data.jpa.repository.support.SimpleJpaRepository.findAll(SimpleJpaRepository.java:78)
     at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
     at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
     at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
     at java.base/java.lang.reflect.Method.invoke(Unknown Source)
     at org.springframework.data.repository.core.support.RepositoryComposition$RepositoryFragments.invoke(RepositoryComposition.java:359)
     at org.springframework.data.repository.core.support.RepositoryComposition.invoke(RepositoryComposition.java:200)
     at org.springframework.data.repository.core.support.RepositoryFactorySupport$ImplementationMethodExecutionInterceptor.invoke(RepositoryFactorySupport.java:644)
     at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
     at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:608)
     at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.lambda$invoke$3(RepositoryFactorySupport.java:595)
     at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:595)
     at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
     at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:59)
     at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
     at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:295)
     at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)
     at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
     at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139)
     … 66 more 


How to reproduce this issue

Default constructor is a constructor method which does not have any arguments. This issue can be reproduced if you block the default constructor in entity java class.

The below example shows the entity class does not have default constructor

package com.yawintutor;

import java.math.BigDecimal;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class Book {

    @Id
    @GeneratedValue
    private Long id;
    private String name;

    public Book(String name) {
    	this.name = name;
    }
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}


Root Cause

All the entity java class should have a default constructor. Default constructor is a constructor method which has no arguments. Hibernate creates entity class using default constructor otherwise throws exception



Solution

From the exception log, identify the entity class that has no default constructor. add a default constructor along with other constructor.

If the default constructor is not a public, change to public default constructor.

The below entity shows the default constructor in a entity java class

package com.yawintutor;

import java.math.BigDecimal;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class Book {

    @Id
    @GeneratedValue
    private Long id;
    private String name;

    public Book() {
    }

    public Book(String name) {
    	this.name = name;
    }
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}



Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *