[jboss-user] [EJB 3.0] - One-to-one and cascade

rafael_rech do-not-reply at jboss.com
Wed Jan 3 14:25:40 EST 2007


Hi all,

I´m trying to set up an one-to-one relationship between two entities using CascadeType.ALL.
The scenario is the following:

1) Entity CidadeNacional has a primary key property called id, some other properties and a property called faixaCep of type FaixaCep configured using the following annotations:
@OneToOne(cascade = CascadeType.ALL)
@PrimaryKeyJoinColumn(name = "id", referencedColumnName = "id")

2) Entity FaixaCep has an id property configured using the following annotations:
    @Id
    @GeneratedValue(generator = "system-foreign")
    @GenericGenerator(name = "system-foreign", strategy = "foreign", parameters = {@Parameter(name = "property", value = "cidade")})
So it shares the PK with entity CidadeNacional

3) Still in FaixaCep there is a property cidade of type CidadeNacional configured using the following annotation
    @OneToOne(mappedBy = "faixaCep", targetEntity=CidadeNacional.class)

It works fine if I first save a CidadeNacional with a FaixaCep.
But, if I first save a CidadeNacional without a FaixaCep, then I load it and try to save it again setting a FaixaCep I get an excpetion.
Here is an extract of my code:
CidadeNacional cidade = new CidadeNacional(...);
cidade.setFaixaCep(null);
entityManager.persist(cidade); // OK
... // Load CidadeNacional
FaixaCep faixa = new FaixaCep(...);
faixa.setId(null);
faixa.setCidade(cidade);
cidade.setFaixaCep(faixa);
entityManager.merge(cidade); // Excpetion

The stack trace is:
Caused by: org.hibernate.id.IdentifierGenerationException: attempted to assign id from null one-to-one property: cidade
    at org.hibernate.id.ForeignGenerator.generate(ForeignGenerator.java:44)
    at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:98)
    at org.hibernate.event.def.DefaultMergeEventListener.entityIsTransient(DefaultMergeEventListener.java:165)
    at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:102)
    at org.hibernate.impl.SessionImpl.fireMerge(SessionImpl.java:689)
    at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:671)
    at org.hibernate.engine.CascadingAction$6.cascade(CascadingAction.java:156)
    at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:213)
    at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:157)
    at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:108)
    at org.hibernate.engine.Cascade.cascade(Cascade.java:248)
    at org.hibernate.event.def.DefaultMergeEventListener.cascadeOnMerge(DefaultMergeEventListener.java:332)
    at org.hibernate.event.def.DefaultMergeEventListener.entityIsPersistent(DefaultMergeEventListener.java:131)
    at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:105)
    at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:51)
    at org.hibernate.impl.SessionImpl.fireMerge(SessionImpl.java:679)
    at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:663)
    at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:667)
    at org.hibernate.ejb.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:201)
    ... 93 more

Could somebody help me?

Thanks


View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3997675#3997675

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3997675




More information about the jboss-user mailing list