org.hibernate.InstantiationException: No default constructor for entity: : principal.Cliente
Asked Answered
L

6

64

I'm having this error:

Exception in thread "main" org.hibernate.InstantiationException: No default constructor for entity:  : principal.Cliente
   at org.hibernate.tuple.PojoInstantiator.instantiate(PojoInstantiator.java:120)
   at org.hibernate.tuple.PojoInstantiator.instantiate(PojoInstantiator.java:136)
   at org.hibernate.tuple.entity.AbstractEntityTuplizer.instantiate(AbstractEntityTuplizer.java:737)
   at org.hibernate.persister.entity.AbstractEntityPersister.instantiate(AbstractEntityPersister.java:4755)
   at org.hibernate.internal.SessionImpl.instantiate(SessionImpl.java:1387)
   at org.hibernate.internal.SessionImpl.instantiate(SessionImpl.java:1375)
   at org.hibernate.loader.plan.exec.process.internal.EntityReferenceInitializerImpl.hydrateEntityState(EntityReferenceInitializerImpl.java:235)
   at org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.readRow(AbstractRowReader.java:107)
   at org.hibernate.loader.plan.exec.internal.EntityLoadQueryDetails$EntityLoaderRowReader.readRow(EntityLoadQueryDetails.java:255)
   at org.hibernate.loader.plan.exec.process.internal.ResultSetProcessorImpl.extractResults(ResultSetProcessorImpl.java:129)
   at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:138)
   at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:102)
   at org.hibernate.loader.entity.plan.AbstractLoadPlanBasedEntityLoader.load(AbstractLoadPlanBasedEntityLoader.java:186)
   at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:4120)
   at org.hibernate.event.internal.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:502)
   at org.hibernate.event.internal.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:467)
   at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:212)
   at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:274)
   at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:150)
   at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1066)
   at org.hibernate.internal.SessionImpl.access$2000(SessionImpl.java:176)
   at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.load(SessionImpl.java:2540)
   at org.hibernate.internal.SessionImpl.get(SessionImpl.java:951)
   at principal.ClienteDAO.obtenContacto(ClienteDAO.java:83)
   at principal.Main.main(Main.java:43)

Have no idea why im getting that exception

EDIT1 here is principal.Cliente

public class Cliente {
    private String name;

    public Cliente(String name) {
        this.name= name;
    }
}

EDIT 2 removed a bunch of useless code to make it look cleaner

Lisa answered 20/5, 2017 at 16:34 Comment(2)
Can you post principal.Cliente class? You need to define no arg constructor in principal.Cliente class.Disqualify
Does this answer your question? Hibernate Annotations : No default constructor for entityFaro
I
137

Missing default constructor in principal.Cliente I can say,

public class Cliente{ 
  public Cliente(){}
}
Immersed answered 20/5, 2017 at 16:37 Comment(3)
Why is the default constructor actually used?Copilot
@KasunSiyambalapitiya #25452518 I found this link to be useful. I had the same question.Peace
@shikhasingh - 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.Duenna
S
9

When you are adding more constructors, make sure to keep the default one.

public Cliente(){
   super();
}

Click here for more detail of why a default constructor is necessary in this scenario.

Si answered 12/9, 2019 at 10:17 Comment(0)
C
5

Notice, in spring boot you should have at least 2 constructors for the models you have.

  1. Constructor empty (no arguments: this is called "default constructor" in java terminology)

    public Cliente(){}

  2. Constructor with all the fields, minus the id.

  3. (facultative) Constructor with all the fields, id included.

You should include the first 2 Constructors in your model. If you remove the first constructor it gives you the error you reported. The 2nd constructor is used by hybernate to generate an instance and save it on the table of the db. The id is generated automatically. The 3rd constructor it is useful for you for making tests with mock data.

Contraoctave answered 2/9, 2022 at 13:18 Comment(0)
L
4

The easiest way to get rid of this error in spring boot is to use the @NoArgsConstructor annotation. For this you will need lombok dependency.I always recommend to use lombok in spring boot projects. Then your entity class will be as follows

import lombok.NoArgsConstructor;

    @Entity
    @NoArgsConstructor
    public class Cliente{ 
      
    }
Lefevre answered 19/12, 2021 at 11:27 Comment(0)
S
1

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.

https://openjpa.apache.org/builds/1.2.3/apache-openjpa/docs/jpa_overview_pc.html

Sciurine answered 1/4, 2023 at 14:26 Comment(0)
T
0

I am learning Spring Boot using Kotlin and I faced the same issue. For me default attributes are a workaround having a default constructor availalble, UserAccount()..

If you need to have an attribute available for mapping only you can use the same solution of having in here roles not to be registered in the SQL table, but instead use authorities.

@Entity
data class UserAccount(
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private val id: Long? = null,
    private val username: String = "",
    private val password: String = "",
    @Transient
    val roles: List<String> = listOf(),
) {
    @ElementCollection(fetch = FetchType.EAGER, targetClass = GrantedAuthority::class)
    val authorities: List<GrantedAuthority> = roles
        .map { role: String ->
            SimpleGrantedAuthority(role)
        }.toList()
}
Tenpin answered 11/2 at 3:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.