No default constructor for entity

The spring boot error No default constructor for entity occurs in an entity class which does not have a default constructor. The hibernate uses the default constructor method to create a bean class. Hibernate fails to create entity classes unless there is a default constructor. Therefore, throws exception org.hibernate.InstantiationException: No entity default constructor.

The default constructor is a method of the constructor without any argument invoked when creating an instance of the class. Hibernate uses default construction to create bean object using reflections. Hibernate uses the setter methods to set the properties of the class. Hibernate is unable to use a bean which has no arg constructor.

Hibernate uses the default constructor to create entity objects. If the default constructor is not available in any of the entities, InstantiationException There was an unexpected error (type=Internal Server Error, status=500). will be thrown from hibernate.

The entity class is nothing but a java bean class which is mapped with a tuples in the database. The entity class contains private class variables with public getters and setters. The entity class variables map to the attributes of the tuples.



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 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)


How to reproduce this issue

The default constructor of a class is a method which does not have any argument, invoked automatically when an instance of the class is created. If a class is created without any default constructor, Hibernate throws this error “No default constructor for entity”.

The example below shows the entity class has no 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

An entity class is nothing but a java bean class. The entity class maps to a tuple in the database. All entity variables are the attributes of the tuples. The entity 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 1

The entity class should have a default constructor. The default constructor is a constructor with no arguments. If the entity doesn’t have the default constructor, add a default constructor along with other constructors.

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


Solution 2

The entity class may be created with default constructor with restricted permission.If the default constructor is not a public, let say private, protected, change to public default constructor. The default constructor should be public to create an instance of the 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;

    private Book() {
    }
    .........
 }

The below example shows with public access specifier in the default constructor.

Solution

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() {
    }
    .........
 }


Solution 3

If the entity class contains constructor with arguments, those constructors are optional to use, remove the constructors with arguments. If there are no constructors in the class, the default constructor will be used during the class instance is created.

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

The constructor created with a argument will be removed to access the 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 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 *