File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Object Relational Mapping and the fly likes Problems with transitive/cascading persistence Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Problems with transitive/cascading persistence" Watch "Problems with transitive/cascading persistence" New topic

Problems with transitive/cascading persistence

Julien Martin
Ranch Hand

Joined: Apr 24, 2004
Posts: 384

I have an entity Poll that can contain multiple entities PossibleAnswer. They are mapped by two tables, one with a pk (Poll) the other with a pk and a fk (PossibleAnswer). It is a bidirectional relationship.

Here is the relevant snippet from PossibleAnswer:

[code] @ManyToOne(cascade=CascadeType.ALL)
private Poll poll;[code]

Here is the relevant snippet from Poll

private Collection<PossibleAnswer> possibleAnswers = new ArrayList<PossibleAnswer>();[code]

I then set the Poll on each possible answer

I then add the possible Answers to the collection.

I then set the collection to the Poll as follows:

and finally I persist the poll.

Only the poll entity gets persisted and therefore the transitive persistence does not work.

I use hibernate as jpa provider.

Can anyone please help?

[ November 19, 2007: Message edited by: Julien Martin ]
Mark Spritzler

Joined: Feb 05, 2001
Posts: 17276

Yes you only set the cascade option for one of the directions, not the other. So set the OneToMany mapping to have a cascade option to save it.


Perfect World Programming, LLC - iOS Apps
How to Ask Questions the Smart Way FAQ
Julien Martin
Ranch Hand

Joined: Apr 24, 2004
Posts: 384
thanks Mark!
I agree. Here's the link:
subject: Problems with transitive/cascading persistence
It's not a secret anymore!