Hibernate version: 3.2.5
Hibernate-Annotations: 3.3.1GA
PostgreSQL: 8.2
I have been trying to create a class (tipoSubestacion) and do it persistent in a table of Postgres (tipo_subestacion) whose is composed by a primary key which formed by two columns: mandt and id, being the id column autogenerated as a sequence in Postgress and mandt is a foreing key.
So far, it has not been possible to create a record in the table whose id is auto generated, hibernate shows a message of Violation of Constraint because doesn’t generate the id, it only put null at id. Otherwise when I set a value on id, hibernate save the data without problems.
The error message is: “java.sql.BatchUpdateException: Batch entry 0 insert into pm.tipo_subestacion (de_tipo_subestacion, id_tipo_subestacion, co_mandt) values (Electromecanica, NULL, 210) was aborted. ”
My question is, it is possible create a compose primary key when one of those is an auto generate column, if it is possible, how?
Here is the tipoSubestacion object with its Annotations:
Code:
@Entity
@IdClass(TipoSubestacionPK.class)
@Table(name="tipo_subestacion", schema="pm"
,uniqueConstraints={@UniqueConstraint(columnNames={"co_mandt","id_tipo_subestacion"})})
public class TipoSubestacion implements ITipoSubestacion
{
@EmbeddedId private Long id;
private IMandante mandante;
private String descripcion;
public TipoSubestacion() {}
public TipoSubestacion( IMandante mandante, String descripcion )
{
this.descripcion = descripcion;
this.mandante = mandante;
}
public TipoSubestacion( Long id, IMandante mandante, String descripcion )
{
this.id = id;
this.descripcion = descripcion;
this.mandante = mandante;
}
@Id
public Long getId()
{
return this.id;
}
public ITipoSubestacion setId( Long id )
{
this.id = id;
return this;
}
@Id
public IMandante getMandante()
{
return this.mandante;
}
public ITipoSubestacion setMandante( IMandante mandante )
{
this.mandante = mandante;
return this;
}
@Column( name = "de_tipo_subestacion", columnDefinition = "VARCHAR(25)")
public String getDescripcion()
{
return this.descripcion;
}
public ITipoSubestacion setDescripcion(String descripcion )
{
this.descripcion = descripcion;
return this;
}
}
Here is the tipoSubestacionPK object with its Annotations:Code:
@Embeddable
@javax.persistence.SequenceGenerator(
name="SEQ_GEN",
sequenceName="pm.tipo_subestacion_id_tipo_subestacion_seq",
allocationSize=1
)
public class TipoSubestacionPK implements ITipoSubestacionPK, Serializable
{
private IMandante mandante = null;
private Long id = null;
//ITipoSubestacion is the interface of TipoSubestacion
private ITipoSubestacion tipoSub;
public TipoSubestacionPK(){}
public TipoSubestacionPK(IMandante mandante)
{
this.mandante = mandante;
}
public TipoSubestacionPK(IMandante mandante, Long id )
{
this.mandante = mandante;
this.id = id;
}
@Valid
@ManyToOne(targetEntity=Mandante.class, fetch = FetchType.LAZY)
@JoinColumn(name = "co_mandt", nullable = false, insertable = true, updatable = true)
public IMandante getMandante()
{
return this.mandante;
}
public ITipoSubestacionPK setMandante(IMandante mandante)
{
this.mandante = mandante;
return this;
}
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="SEQ_GEN")
@Column( name = "id_tipo_subestacion", columnDefinition = "INTEGER")
public Long getId()
{
return this.id;
}
public ITipoSubestacionPK setId(Long id)
{
this.id = id;
return this;
}
@OneToOne(mappedBy="tipo_subestacion")
public ITipoSubestacion getTipoSubestacion()
{
return tipoSub;
}
}
And here is the definition of the PostgreSQL sequence, according to pgAdmin:
CREATE SEQUENCE pm.tipo_subestacion_id_tipo_subestacion_seq
INCREMENT 1
MINVALUE 1
MAXVALUE 9223372036854775807
START 2
CACHE 1;
ALTER TABLE pm.tipo_subestacion_id_tipo_subestacion_seq OWNER TO postgres;