I tiene este dos tabla de la base de datos de revisions
y Pagu
una clave Hibernate 3 Compuesto con GeneratedValue
en el modelo Pagu
, tengo que clave compuesta:
- ID int (autogenerado por base de datos)
- REVISION_ID (foreign_key a revisiones) tabla
cómo implementar esto en Hibernate 3?
Esto es lo que ocurrió con
@Entity
@Table(name="pagu"
,schema="dbo"
,catalog="dbname"
)
@IdClass(PaguId.class)
public class Pagu implements java.io.Serializable {
private int id;
private int revisiId;
private Entitas entitas;
private Revisi revisi;
...
@Id
@GeneratedValue
@Column(name="id", unique=true, nullable=false)
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
@Id
@Column(name="revisi_id", unique=true, nullable=false)
public int getRevisiId() {
return this.revisiId;
}
public void setRevisiId(int id) {
this.id = id;
}
Y esta es mi clase PaguId
@Embeddable
public class PaguId implements java.io.Serializable {
private int id;
private int revisiId;
public PaguId() {
}
public PaguId(int id, int revisiId) {
this.id = id;
this.revisiId = revisiId;
}
@Column(name="id", nullable=false)
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
@Column(name="revisi_id", nullable=false)
public int getRevisiId() {
return this.revisiId;
}
public void setRevisiId(int revisiId) {
this.revisiId = revisiId;
}
public boolean equals(Object other) {
if ((this == other)) return true;
if ((other == null)) return false;
if (!(other instanceof PaguId)) return false;
PaguId castOther = (PaguId) other;
return (this.getId()==castOther.getId() && this.getRevisiId()==castOther.getRevisiId())
&& (this.getRevisiId()==castOther.getRevisiId());
}
public int hashCode() {
int result = 17;
result = 37 * result + this.getId();
result = 37 * result + this.getRevisiId();
return result;
}
}
Cuando intento para salvar esto en la base de datos que ha obtenido un error:
org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session:
- ACTUALIZACIÓN-- Pero cambiar la implementación usando EmbeddedId como este
public class Pagu implements java.io.Serializable {
private PaguId id;
...
@EmbeddedId
@AttributeOverrides({
@AttributeOverride(name="id", [email protected](name="id", nullable=false)),
@AttributeOverride(name="revisiId", [email protected](name="revisi_id", nullable=false)) })
public PaguId getId() {
return this.id;
}
public void setId(PaguId id) {
this.id = id;
}
....
Compiló a la derecha, pero me dio error cuando persistió el modelo.
org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): id.model.Pagu
No debe usar '@ Embeddable' y' @ IdClass' al mismo tiempo. Elija utilizar: 1. '@ Embeddable' en la clase de clave primaria compuesta y' @ EmbeddedId' en la clase de entidad, o 2. no hay anotación en la clase de clave primaria compuesta y '@ IdClass' en el nivel de clase de entidad con múltiple Anotaciones '@ Id' en la propia clase de entidad (para campos). –
Haciendo esto también se generó la excepción 'un objeto diferente con el mismo valor de identificador ya estaba asociado con la sesión: [id.go.model.Pagu # id.go..model.PaguId @ 5ae9]' el error amable que obtuve si hubiera no es @GeneratedValue en @id – ahmy
Encontré un enlace para crear una generación de identificador parcial http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/#d0e1150 pero también hay una advertencia que tal construcción como fundamentalmente errónea – ahmy