aspose file tools*
The moose likes JBoss/WildFly and the fly likes One-to-one and cascade Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » JBoss/WildFly
Bookmark "One-to-one and cascade" Watch "One-to-one and cascade" New topic
Author

One-to-one and cascade

rafa rech
Greenhorn

Joined: Jun 17, 2003
Posts: 7
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


Tks,<br /> Rafael.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

Moving to the ORM forum.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: One-to-one and cascade