Hello. I have got 4 classes. One superclass and three subclasses. I use a JOINED strategy.
The problem comes with one of the subclasses. If I don't use it or there is no data on the table which represents, all goes OK, but if I retrieve all the instances of the abstract superclass, an error appears as follows:
Cannot instantiate class or interface: es.udc.madtyn.model.puesto.Puesto
Here it goes the code. I can't understand why it works with two subclasses (Monitor and Administrativo), but it doesn't work with the analogous third subclass Mantenimiento.
Any help will be appreciated. Thanks.
Code:
/*SUPERCLASS*/
@Entity
@Inheritance(strategy=InheritanceType.JOINED)
@PrimaryKeyJoinColumn(name="idPuesto")
public abstract class Puesto implements Serializable {
private Long idPuesto;
private double salBase;
private long version;
public Puesto() {}
public Puesto(double salBase) {
this.salBase = salBase;
}
@SequenceGenerator(name="IdPuestoGenerator",
sequenceName="PuestoSeq")
@Id
@GeneratedValue(strategy=GenerationType.AUTO,
generator="IdPuestoGenerator")
public Long getIdPuesto() {
return idPuesto;
}
public void setIdPuesto(Long idPuesto) {
this.idPuesto = idPuesto;
}
public double getSalBase() {
return salBase;
}
public void setSalBase(double salBase) {
this.salBase = salBase;
}
@Version
public long getVersion() {
return version;
}
public void setVersion(long version) {
this.version = version;
}
/* HASHCODE AND EQUALS METHODS*/
@Transient
public String getLabel() {
return "";
}
}
/* THE TWO CORRECT SUBCLASSES ARE DOWN HERE, FOLLOWED BY THE WRONG ONE */
@Entity
@PrimaryKeyJoinColumn(name = "idPuesto")
public class Administrativo extends Puesto {
private String cargo;
private double plus;
public Administrativo() {
super();
}
public Administrativo(double salBase, String cargo, double plus) {
super(salBase);
this.cargo = cargo;
this.plus = plus;
}
public String getCargo() {
return cargo;
}
public void setCargo(String cargo) {
this.cargo = cargo;
}
public double getPlus() {
return plus;
}
public void setPlus(double plus) {
this.plus = plus;
}
/* HASHCODE AND EQUALS METHODS*/
@Transient
@Override
public String getLabel() {
return "Administrativo: " + cargo;
}
}
@Entity
@PrimaryKeyJoinColumn(name = "idPuesto")
public class Monitor extends Puesto {
private String especialidad;
private double plus;
public Monitor() {
super();
}
public Monitor(double salBase, String especialidad, double plus) {
super(salBase);
this.especialidad = especialidad;
this.plus = plus;
}
public String getEspecialidad() {
return especialidad;
}
public void setEspecialidad(String especialidad) {
this.especialidad = especialidad;
}
public double getPlus() {
return plus;
}
public void setPlus(double plus) {
this.plus = plus;
}
/* HASHCODE AND EQUALS METHODS*/
@Transient
@Override
public String getLabel() {
return "Monitor: " + especialidad;
}
}
/* THE NO WORKING SUBCLASS IS DOWN HERE*/
@Entity
@PrimaryKeyJoinColumn(name = "idPuesto")
public class Mantenimiento extends Puesto {
private String denom;
private double plus;
public Mantenimiento() {
super();
}
public Mantenimiento(double salBase, String denom, double plus) {
super(salBase);
this.denom = denom;
this.plus = plus;
}
@Column(name="denom")
public String getDenom() {
return denom;
}
public void setDenom(String denom) {
this.denom = denom;
}
@Column(name="plus")
public double getPlus() {
return plus;
}
public void setPlus(double plus) {
this.plus = plus;
}
/* HASHCODE AND EQUALS METHODS*/
@Transient
@Override
public String getLabel() {
return "Mantenimiento: " + denom;
}
}