org.hibernate.InstantiationException: Cannot instantiate abstract class or interface
For others that the above solutions might not work, give this a thought and a try afterwards: When you have you child entities inherit parent entity you will not be able to instantiate the Parent itself but you should instantiate the child that has the parents properties - For example i had Parent abstract class Profile and i had a child class StudentProfile that extended it. I tried instantiating a list of profiles and it threw the exception(I had data in the profile table in the database but i forgot to add data to the StudentProfile table(and connect them through the for foreign key column - in my case id) My solution was to add the corresponding data to the StudentProfile table and then i was able to get rid of the exception. Hope this helps!!
Ok, so it turns out some of the entities that inherited from Compensation
had properties with ManyToOne
relations and they were missing cascade annotations. Because they were more than one property, I received no TransientPropertyValueException
and instead it tried to load the parent Compensation
entity which was the closest thing that it could associate the entity to without those fields that weren't being cascaded. Which means that this whole problem was due to missing cascade annotations.
For example:
@Entity
@Table(name = "davt_compensation_service_relocation")
public class RelocationCompensation extends Compensation<RelocationCompensation> {
@Valid
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "phone_id")
@ForeignKey(name = "FK_relocationCompensation_phone")
private Phone phone = new Phone();
@Valid
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "address_id")
@ForeignKey(name = "FK_relocationCompensation_address")
private Address address = new Address();
//rest of properties, setters and getters, equals and hashcode overrides
}
Changed to:
@Entity
@Table(name = "davt_compensation_service_relocation")
public class RelocationCompensation extends Compensation<RelocationCompensation> {
@Valid
@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name = "phone_id")
@ForeignKey(name = "FK_relocationCompensation_phone")
private Phone phone = new Phone();
@Valid
@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name = "address_id")
@ForeignKey(name = "FK_relocationCompensation_address")
private Address address = new Address();
//rest of properties, setters and getters, equals and hashcode overrides
}
Since this is the only question that comes up you google search this error, I wanted to share another potential solution. I had an issue when my abstract class used Single Table inheritance and gave the 'Cannot instantiate abstract class or interface' exception when I tried to read my pre-loaded data.
My persistence.xml contained the abstract class, but it did not include the classes that extend the abstract class. Since the code to read the classes are not directly referenced they do not throw a invalid entity class exception. When it tried to instantiate the class based on the DiscriminatorColumn value it threw this error. Adding the implementation class to persistence.xml fixed this issue.