ho questo due tavoli sul database revisions
e Pagu
Hibernate 3 composita una chiave con GeneratedValue
nel modello Pagu
, devo chiave composta:
- id int (generata automaticamente da database)
- REVISION_ID (foreign_key a revisioni) tavolo
come implementare questo su Hibernate 3?
questo è ciò che mi si avvicinò 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;
}
E questa è la mia classe 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;
}
}
Quando cerco di salvare questo sulla base di dati ho ottenuto l'errore:
org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session:
- UPDATE-- Ma cambiare l'implementazione usando EmbeddedId come questo
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;
}
....
Compilava correttamente, ma mi dava un errore quando persisteva il modello.
org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): id.model.Pagu
Non dovresti usare '@ Embeddable' e' @ IdClass' allo stesso tempo. O scegliere di utilizzare: 1. '@ Embeddable' sulla classe di chiave primaria composta e' @ EmbeddedId' nella classe di entità, o 2. nessuna annotazione sulla classe di chiave primaria composta e '@ IdClass' a livello di classe di entità con più Annotazioni '@ Id' nella stessa classe di entità (per i campi). –
Anche questo ha generato un'eccezione 'un oggetto diverso con lo stesso valore identificatore era già associato alla sessione: [id.go.model.Pagu # id.go..model.PaguId @ 5ae9]' l'errore gentile che ho ricevuto se non è @GeneratedValue su @id – ahmy
Ho trovato un collegamento per creare la generazione di identificatori parziali http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/#d0e1150 ma c'è anche un avviso che un tale costrutto come fondamentalmente sbagliato – ahmy