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: one-to-one and one-to-many to the same column and entity
PostPosted: Fri Jun 15, 2007 1:02 pm 
Newbie

Joined: Wed Oct 06, 2004 4:39 pm
Posts: 17
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.


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.