Site icon NgDeveloper

[Solved] org.hibernate.MappingException: Could not get constructor for org.hibernate.persister.entity.SingleTableEntityPersister

[Solved] org.hibernate.MappingException: Could not get constructor for org.hibernate.persister.entity.SingleTableEntityPersister

Default Constructor

Java pojo mapping classes must have the empty/default constructor. Creating an empty or default constructor for all your java hibernate entity classes are the recommended coding style. Because you can resolve these kind of exceptions “org.hibernate.MappingException: Could not get constructor for org.hibernate.persister.entity.SingleTableEntityPersister” whenever hibernate expects default constructor for your entity classes.

Recommended way (Entity with default constructor)

public GoalPoints() { // do nothing }
@Entity
@Table(name = "goal_points")
public class GoalPoints implements Serializable {
 
 private static final long serialVersionUID = 1L;

 public GoalPoints() {
  // do nothing
 }
}

Not recommended: (Entity without default constructor)

@Entity
@Table(name = "goal_points")
public class GoalPoints implements Serializable {
 
 private static final long serialVersionUID = 1L;
}

Generate getters & setters

Make sure you have generated getters and setters for all your fields.

Do not write yourself, always use IDE’s to generate the getters and setters for all your fields.

In Eclipse, Open you Entity class -> Right click -> Source -> Generate Getters and Setters

Generate Getters and Setters Eclipse

Mapping between Table and Java through (hbm / entity)

Make sure you always use the entities rather than hibernate mapping files (hbm xml files). 

If you are using mapping files (.hbm.xml),

then make sure you have kept the same names for both java mapping class and the property binding in mapping files (.hbm.xml)

 If you are using entities,

then you may not be required to perform this mapping check. Because if your entity is not matching with your table, then you will get the exception during start up itself.

Hashcode and Equals

Generate hashcode and equals for the required fields (primary columns of the table and constraint check fields) of your java entity classes.

Do remember, this is also for a recommendations only, and this exception will not happen due to this case.

In Short

This hibernate mapping exception will come for any of the below reasons can be,

  1. Your Java POJO Mapping class may not have the empty/default constructor.
  2. Your Set/List or any collection class may not have the default constructor or overridden hashcode and equals method.
  3. Set name you have given in the java class and mapping .hbm.xml file may be different.

Please add your comments and feed backs in the below comments section.

Must to know Hibernate stuffs:

Exit mobile version