Hibernate version:
3.2.3
I have two entities with two associations between them:
* a one-to-one association
* a one-to-many association
Mapping documents:
@Entity
@Table(name = "envase")
public class Envase {
private EstadoEnvase estadoActual;
private Set<EstadoEnvase> estados = new HashSet<EstadoEnvase>();
@OneToOne
@JoinColumn(name = "id_estadoactual")
@org.hibernate.annotations.ForeignKey(name = "fk_envase_estadoenvase")
public EstadoEnvase getEstadoActual() {
return estadoActual;
}
@OneToMany(mappedBy = "envase")
@org.hibernate.annotations.Cascade( {
org.hibernate.annotations.CascadeType.ALL,
org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
public Set<EstadoEnvase> getEstados() {
return estados;
}
}
public class EstadoEnvase {
private Envase envase;
@ManyToOne
@JoinColumn(name = "id_envase", nullable = false)
public Envase getEnvase() {
return envase;
}
}
I have problems to create an instance of Envase and assign a initial state.
Envase envase = new Envase();
EstadoEnvase estadoInicial = new EstadoEnvase();
estadoInicial.setEnvase(envase);
envase.getEstados().add(estadoInicial);
envase.setEstadoActual(estadoInicial);
I obtain the exception "... object references unsaved transient instance ...", because the instance of EstadoEnvase is transient.
I change the OneToOne association:
@OneToOne --> @OneToOne(cascade = CascadeType.PERSIST)
I resolve the last problem, but I obtain the exception:
"... not-null property references a null or transient value ...", because the property envase of EstadoEnvase is null.
If I change to the association OneToOne:
@OneToOne(cascade = CascadeType.PERSIST) --> @OneToOne(cascade = CascadeType.PERSIST, mappedBy = "envase"), the exception is gone, but the column "id_estadoactual" is null.
Is possible this mapping?
Thanks.
|