This “InstantiationException: No default constructor for entity” is very common for all Hibernate or Spring Boot JPA application development. In this post you will learn about the problem and solutions to resolve it.
Reason of InstantiationException “No default constructor for entity”
The JPA specification requires that all persistent classes have a no-arg constructor. This constructor may be public or protected. Because the compiler automatically creates a default no-arg constructor when no other constructor is defined, only classes that define constructors must also include a no-arg constructor.
In the next sections of post, you will learn about the more detail about the issue and solutions by different ways.
Example
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
@Entity
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
//parameterize constructor
public Employee(String name) {
super();
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;
}
@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + "]";
}
}
Stacktrace
Caused by: org.springframework.orm.jpa.JpaSystemException: No default constructor for entity : com.facingissuesonit.model.Employee
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:320) ~[spring-orm-6.0.11.jar:6.0.11]
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:771) ~[spring-boot-3.1.2.jar:3.1.2]
... 8 common frames omitted
Caused by: org.hibernate.InstantiationException: No default constructor for entity : com.facingissuesonit.model.Employee
at org.hibernate.metamodel.internal.EntityInstantiatorPojoStandard.instantiate(EntityInstantiatorPojoStandard.java:93) ~[hibernate-core-6.2.6.Final.jar:6.2.6.Final]
... 23 common frames omitted
Problem
The above Employee entity class is not having the default no-arg constructor, due to JPA specification it’s required and through above InstantionException or JPASystemException.
Solutions
There are many ways to resolve this issue:
Solution 1: Add Default Constructor
You can create Default Constructor as below with public or protected scope.,
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
@Entity
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
//Default Constructor
public Employee() {
}
//parameterize constructor
public Employee(String name) {
super();
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;
}
@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + "]";
}
}
Solution 2: Lombok Annotations
Lombok provide good annotations to reduce effort of development and clean code, once you use the annotation it’s automatically generate the code on compile classes. Specific to this issue you can use the above class as below with different annotations.
See Also:
@NoArgsConstructor
When use @NoArgsConstructor the Lombok will generate default constructor of class on compile time. Ex:
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
@NoArgsConstructor
@Entity
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
//Default Constructor automatically created by lombok
//public Employee() {
//}
//parameterize constructor
public Employee(String name) {
super();
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;
}
@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + "]";
}
}
@Data
Lombok @Data annotation more simplified and make your class for boiler plate code. Lombok annotation generate the code for required methods on compile time. Now you can see this class is more clean because no need to write code for getter & setter methods, default constructor, parameterize constructor , toString() method.
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
@Data
@Entity
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
//all getter, setter methods, default constructors, parameterize constructor, toString() method for all above fields will create by lombok on compile class.
}
Conclusion
In this post you have learned about the reason of “InstantiationException: No default constructor for entity” and different solutions to resolve this issue.
References
- https://stackoverflow.com/questions/44088360/org-hibernate-instantiationexception-no-default-constructor-for-entity-princ
- https://openjpa.apache.org/builds/1.2.3/apache-openjpa/docs/jpa_overview_pc.html
Related Posts
Your Feedback Motivate Us
If our FacingIssuesOnIT Experts solutions guide you to resolve your issues and improve your knowledge. Please share your comments, like and subscribe to get notifications for our posts.
Happy Learning !!!
You must log in to post a comment.