-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Derived Identifiers with JPA / Hibernate
PostPosted: Tue Jul 27, 2010 6:29 am 
Newbie

Joined: Tue Jul 27, 2010 6:07 am
Posts: 1
I have a compound primary key formed by the next params :

@Entity
@Table(name = "cita")
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "CITA_TYPE", discriminatorType = DiscriminatorType.STRING)
@IdClass(CitaID.class)
public class CitaEntity extends EntityObject {

private static final long serialVersionUID = 1L;
@Id
private String idCita;
@ManyToOne
private HistorialEntity historial;
...
/**
* Constructor por defecto sobrecargado con los elementos minimos necesarios
*/

public CitaEntity(CitaID identificador, EstadoCita estado,MedicoEntity medico) {
this.idCita = identificador.getIdCita();
this.estado = estado;
this.medico = medico;
this.historial = new HistorialEntity();

}
...

I have defined citaID this way :


public class CitaID extends EntityObject {

private static final long serialVersionUID = 1L;
private String idCita;
private int historial;

public CitaID() {
idCita = "";
historial = 0;
}

public CitaID(String idCita, int historial) {
this.idCita = idCita;
this.historial = historial;
}

public boolean equals(Object o){
return ((o instanceof CitaID) &&
(idCita.equals(((CitaID)o).getIdCita())) &&
(historial == ((CitaID)o).getHistorial()));
}

public int hashCode(){
return idCita.hashCode()+historial;
}
public String getIdCita() {
return idCita;
}

public int getHistorial() {
return historial;
}
}

and the class "Historial" has the relation ...

@OneToMany(mappedBy="historial")
private List<CitaEntity> listaCitas;

this works in the way that the class "cita" has many "historial", having a compound primary key . My problem appears with the constructor of citaEntity . Do I use the "CitaID" class ( the compound identifier) or I use the attributes ? . Finding an element works fine :

identificador = new CitaID("HOSP150620101504", 27);
cita = em.find(CitaEntity.class, identificador);

but when I try to create a new element cita and persist it, it crashes with this message :

// identificador its the compund primary key
cita = new CitaHospitalariaEntity(identificador,
EstadoCita.pendiente_asignacion, hospital, medico);

...


org.hibernate.PropertyAccessException: could not set a field value by reflection setter of gestor.REH.entidades.CitaID.historial


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.