Hi,
I'm trying to map a class hierarchy of three levels using the single table strategy with joined tables for middle classes (level 2 on hierarchy).
These are my class definitions (w/o getters&setters)
First level class:
Code:
@Entity
@Table(name = Operacion.TABLE_NAME)
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "TIPO", discriminatorType = DiscriminatorType.STRING)
public abstract class Operacion {
public static final String TABLE_NAME = "TB_OPERACION";
@Id
@Column(name="ID_OPERACION")
@SequenceGenerator(name="op_seq", sequenceName="TB_OPERACION_ID_GENERATOR")
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="op_seq")
private Long idOperacion;
@Column(name="ASUNTO", length=1024)
private String asunto;
@Column(name="FECHA_INICIO_OPERACION")
private Timestamp fechaInicioOperacion;
@Column(name="FECHA_FIN_OPERACION")
private Timestamp fechaFinOperacion;
@Column(name="RESULTADO")
private int resultado;
@Column(name="USUARIO", length=10)
private String usuario;
}
Second level class:
Code:
@Entity
@SecondaryTable(name=Peticion.TABLE_NAME, pkJoinColumns = @PrimaryKeyJoinColumn(name="ID_OPERACION"))
public abstract class Peticion extends Operacion {
public static final String TABLE_NAME = "TB_OP_PETICION";
@Column(table=TABLE_NAME, name="ESTADO")
private int estado;
}
Third level class:
Code:
@Entity
@SecondaryTable(name = Peticion.TABLE_NAME, pkJoinColumns = @PrimaryKeyJoinColumn(name="ID_OPERACION"))
public class PeticionCambioEstado extends Peticion {
@Column(table=Peticion.TABLE_NAME, name="CE_COMENTARIO", length=1024)
private String comentario;
@Column(table=Peticion.TABLE_NAME, name="CE_NUEVO_ESTADO")
private int nuevoEstado;
}
With these mappings when I tried to save a PeticionCambioEstado I get an error cause hibernate does three inserts:
Code:
Hibernate: insert into TB_OPERACION (ASUNTO, FECHA_FIN_OPERACION, FECHA_INICIO_OPERACION, RESULTADO, USUARIO, TIPO, ID_OPERACION) values (?, ?, ?, ?, ?, 'PeticionCambioEstado', ?)
Hibernate: insert into TB_OP_PETICION (ESTADO, ID_OPERACION) values (?, ?)
Hibernate: insert into TB_OP_PETICION (CE_COMENTARIO, CE_NUEVO_ESTADO, ID_OPERACION) values (?, ?, ?)
Can someone tell me if this mapping approach for inheritance is valid? If yes, what am I doing wrong?
Thank you!